使用 jQuery 遍历元素

Looping through elements with jQuery

我正在尝试循环浏览某些导航 HTML,因为我需要动态修改某些项目。具体来说,我将如何使用 class Droppable 遍历每个项目并获得某些 children 的 jQuery object。我在下面发布了我的代码,上面有一堆星号 (*) 表示我需要操作的东西的注释 jquery objects.

<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() is a jQuery method and you can't call it on DOM object 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");
});