gpt4 book ai didi

JavaScript 切换不起作用,控制台中没有错误

转载 作者:行者123 更新时间:2023-12-03 02:53:09 27 4
gpt4 key购买 nike

我试图在单击 .menu-item-2891 时添加一个下拉菜单。添加类 .active 将显示菜单。现在,它不会在单击时执行任何操作,并且在控制台中不会显示任何错误。

$(".menu-item-2891").children('.nav-top-link').click(function() {
$(".menu-item-2891").toggleClass("active");
});
.active {
background: Red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="menu-item-2891" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-2891 has-child" aria-expanded="false"><a class="nav-top-link">MEN</a>
<button class="toggle"><i class="icon-angle-down"></i></button>
<ul class="children">
<li id="menu-item-9027" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9027"><a href="#">SHOP BY CATEGORY</a>
<ul class="nav-sidebar-ul">
<li id="menu-item-10798" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-10798"><a href="https://squatwolf.staging.wpengine.com/product-category/men/">VIEW ALL PRODUCTS</a></li>
<li id="menu-item-9019" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9019"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-tshirt/">T-SHIRTS</a></li>
<li id="menu-item-9020" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9020"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-stringers-tanks/">STRINGERS &amp; TANKS</a></li>
<li id="menu-item-9025" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9025"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-hoodies/">HOODIES</a></li>
<li id="menu-item-9023" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9023"><a href="https://squatwolf.staging.wpengine.com/product-category/jogger-pants/">JOGGER PANTS</a></li>
<li id="menu-item-9024" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9024"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-shorts/">SHORTS</a></li>
<li id="menu-item-9973" class="label-new menu-item menu-item-type-custom menu-item-object-custom menu-item-9973"><a href="https://squatwolf.staging.wpengine.com/product-category/men-gym-leggings/">MEN’S LEGGINGS / TIGHTS</a></li>
</ul>
</li>
<li id="menu-item-9028" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9028"><a href="#">SHOP BY SERIES</a>
<ul class="nav-sidebar-ul">
<li id="menu-item-9021" class="label-new menu-item menu-item-type-custom menu-item-object-custom menu-item-9021"><a href="https://squatwolf.staging.wpengine.com/product-category/men-new-arrivals/">NEW ARRIVALS</a></li>
<li id="menu-item-9029" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9029"><a href="https://squatwolf.staging.wpengine.com/product-category/series/ribbed-series/">RIBBED SERIES</a></li>
<li id="menu-item-9035" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9035"><a href="https://squatwolf.staging.wpengine.com/product-category/series/series-2-0/">SERIES 2.0</a></li>
<li id="menu-item-10509" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-10509"><a href="https://squatwolf.staging.wpengine.com/product-category/series/seamless/">SEAMLESS SERIES</a></li>
<li id="menu-item-9026" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9026"><a href="https://squatwolf.staging.wpengine.com/product-category/sale/">SALE</a></li>
</ul>
</li>
</ul>
</li>

最佳答案

给你:

$(".menu-item-2891").children('.nav-top-link').click(function() {
$(".menu-item-2891").toggleClass("active");
});
.active {
background: Red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li id="menu-item-2891" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-2891 has-child" aria-expanded="false"><a class="nav-top-link">MEN</a>
<button class="toggle"><i class="icon-angle-down"></i></button>
<ul class="children">
<li id="menu-item-9027" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9027"><a href="#">SHOP BY CATEGORY</a>
<ul class="nav-sidebar-ul">
<li id="menu-item-10798" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-10798"><a href="https://squatwolf.staging.wpengine.com/product-category/men/">VIEW ALL PRODUCTS</a></li>
<li id="menu-item-9019" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9019"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-tshirt/">T-SHIRTS</a></li>
<li id="menu-item-9020" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9020"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-stringers-tanks/">STRINGERS &amp; TANKS</a></li>
<li id="menu-item-9025" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9025"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-hoodies/">HOODIES</a></li>
<li id="menu-item-9023" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9023"><a href="https://squatwolf.staging.wpengine.com/product-category/jogger-pants/">JOGGER PANTS</a></li>
<li id="menu-item-9024" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9024"><a href="https://squatwolf.staging.wpengine.com/product-category/gym-shorts/">SHORTS</a></li>
<li id="menu-item-9973" class="label-new menu-item menu-item-type-custom menu-item-object-custom menu-item-9973"><a href="https://squatwolf.staging.wpengine.com/product-category/men-gym-leggings/">MEN’S LEGGINGS / TIGHTS</a></li>
</ul>
</li>
<li id="menu-item-9028" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-9028"><a href="#">SHOP BY SERIES</a>
<ul class="nav-sidebar-ul">
<li id="menu-item-9021" class="label-new menu-item menu-item-type-custom menu-item-object-custom menu-item-9021"><a href="https://squatwolf.staging.wpengine.com/product-category/men-new-arrivals/">NEW ARRIVALS</a></li>
<li id="menu-item-9029" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9029"><a href="https://squatwolf.staging.wpengine.com/product-category/series/ribbed-series/">RIBBED SERIES</a></li>
<li id="menu-item-9035" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9035"><a href="https://squatwolf.staging.wpengine.com/product-category/series/series-2-0/">SERIES 2.0</a></li>
<li id="menu-item-10509" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-10509"><a href="https://squatwolf.staging.wpengine.com/product-category/series/seamless/">SEAMLESS SERIES</a></li>
<li id="menu-item-9026" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9026"><a href="https://squatwolf.staging.wpengine.com/product-category/sale/">SALE</a></li>
</ul>
</li>
</ul>
</li>

关于JavaScript 切换不起作用,控制台中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47751740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com