[英]how to add toggle function to sub children of <li> in jquery
when i am trying to add toggle function to the child nodes nothing is happening could you help me out. 当我尝试将切换功能添加到子节点时,什么也没发生,您能帮帮我吗。
$(document).ready(function() { $('label.tree-toggler').click(function() { $(this).parent().children('ul.tree').toggle(300); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav nav-list"> <li><label class="tree-toggler nav-header">WorkLoad</label> <ul class="nav nav-list tree"> <li><label class="tree1">DME Report</label> <ul class="tree1"> <li> <a href="search.php">Report1</a> <a href="search.php">Report2</a> </li> </ul> </li> <li><a href="update.php">CAMB Report</a></li> <li><a href="index2.php">LMAB Report</a></li> <li><a href="#">DMF Notification</a></li> <li><a href="#">LME Forecast Report</a></li> </ul> </li> </ul>
This would allow to simplify the above code to: 这将使上面的代码简化为:
The code below mostly works as intended, ...
下面的代码通常可以按预期工作,...
$(document).ready(function() { $('label.tree-toggler, label.tree1').click(function() { $(this).parent().children('ul.tree , ul.tree1').toggle(300); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav nav-list"> <li><label class="tree-toggler nav-header">WorkLoad</label> <ul class="nav nav-list tree"> <li><label class="tree1">DME Report</label> <ul class="tree1"> <li> <a href="search.php">Report1</a> <a href="search.php">Report2</a> </li> </ul> </li> <li><a href="update.php">CAMB Report</a></li> <li><a href="index2.php">LMAB Report</a></li> <li><a href="#">DMF Notification</a></li> <li><a href="#">LME Forecast Report</a></li> </ul> </li> </ul>
You can make this work in a generic way by attaching the click event handler only to li
elements which have child ul
using the :has
selector. 您可以使用
:has
选择器,将click事件处理程序仅附加到具有子ul
li
元素上,以通用方式进行此工作。 Then you can toggle()
those ul
within the click handler. 然后,您可以在点击处理程序中
toggle()
那些ul
。
Also note the handler will need to call stopPropagation()
on the event to stop the parent li
from closing their child ul
elements too. 还要注意,处理程序将需要在事件上调用
stopPropagation()
,以阻止父li
关闭其子ul
元素。 Try this: 尝试这个:
$(document).ready(function() { $('ul > li:has(ul)').click(function(e) { e.stopPropagation(); $(this).find('> ul').toggle(300); }); });
ul > li > ul { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav nav-list"> <li> <label class="nav-header">WorkLoad</label> <ul class="nav nav-list tree"> <li> <label class="tree1">DME Report</label> <ul class="tree1"> <li> <a href="search.php">Report1</a> <a href="search.php">Report2</a> </li> </ul> </li> <li><a href="update.php">CAMB Report</a></li> <li><a href="index2.php">LMAB Report</a></li> <li><a href="#">DMF Notification</a></li> <li><a href="#">LME Forecast Report</a></li> </ul> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.