gpt4 book ai didi

jquery - 切换直接子级的类

转载 作者:行者123 更新时间:2023-12-01 04:39:25 25 4
gpt4 key购买 nike

我有几个具有相同类的 div。我希望能够通过单击功能独立地切换每个子级的类。

if($("div").hasClass("subLinkPage")) {
$(".sub-expand_collapse").children(".fa").toggleClass('fa-minus fa-plus');
}

我应该向此脚本添加什么,以便它仅在每个脚本的直接子级上运行。

感谢您的建议

HTML

  <div class="subLinkPage"> 
<ul aria-labelledby="headLnk_02" class="nav__items">
<li class="sub_p01 togSubList">

<a role="button" href="#" class="sub-expand_collapse" aria-label="toggle menu">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>

<a id="subLnk_01" class="parent_link menu_p01" href="../content/m04/m04_c02_p01.html">Facebook feed</a>
<ul class="sub_items sub_items_01">
<li><a href="../content/m04/m04_c02_p01-a.html" class="menu_p01a">Adding a Facebook feed<br/>to the home page</a></li>
</ul>
</li>
<li class="sub_p02 togSubList">


<a role="button" href="#" class="sub-expand_collapse" aria-label="toggle menu">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>

<a id="subLnk_02" class="parent_link menu_p02" href="../content/m04/m04_c02_p02.html">Twitter feed</a>
<ul class="sub_items sub_items_02">
<li><a href="../content/m04/m04_c02_p02-a.html" class="menu_p02a">Adding a Twitter<br/>feed web part</a></li>
</ul>
</li>
</ul>
</div>

最佳答案

您可以使用直接子级 jQuery selector (父>子)

$("div.subLinkPage").on('click', function() {
$(this).find(".sub-expand_collapse > .fa").toggleClass('fa-minus fa-plus');
});

关于jquery - 切换直接子级的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262859/

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