[英]Looping through elements with jQuery
我试图遍历一些导航HTML,因为我需要动态修改一些项目。 具体来说,我将如何通过Droppable类遍历每个项目并为某些子项获取jQuery对象。 我在下面的代码中贴了一些星号(*),表示需要作为jquery对象操作的事物的注释。
<nav id="sitenav">
<ul class="container ul-reset">
<li class="droppable "> ****** foreach of these
<a class="RootNode" id="Help" href="javascript:;">HELP</a> ****** I need this
<div class="mega-menu">
<div class="container cf">
<div style="display: inline-block;">
<ul class="ul-reset"> ****** and I need this
<a class="heading disabled" id="Help_Help" href="javascript:;">
<h3>Help</h3>
</a>
<a id="ContactUs" href="/ContactUs/">Contact Us</a>
<a id="UserGuides" href="/Help/">User Guides</a>
</ul>
</div>
</div>
</div>
</li>
{more lis with the same structure...}
</ul>
</nav>
我尝试了以下操作,但出现一个错误,因为它没有find方法,我认为这是因为我认为这将是每个循环中当前jQuery包装的DOM元素。
$("li.droppable").each(function (index) {
var header = this.find("a.RootNode");
var col = this.find("ul.ul-reset");
});
.find()
是一个jQuery方法,您不能在DOM对象this
上调用它。
您可以改为对jQuery对象$(this)
调用.find()
方法,因此应为:
$("li.droppable").each(function(index) {
var $this = $(this);
var header = $this.find("a.RootNode");
var col = $this.find("ul.ul-reset");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.