简体   繁体   中英

Looping through elements with jQuery

I am trying to loop through some navigation HTML because I need to modify some items dynamically. Specifically how would I loop through each item with class Droppable and get the jQuery object for certain children. I have posted my code below with a bunch of asterisks(*) denoting notes of the things I need to manipulate as 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>

I tried the below but I get an error that this doesn't have a find method, which I thought it would because I thought this would be the current jQuery wrapped DOM element from the each loop.

$("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 .

You could instead call the .find() method on jQuery object $(this) , so it should be :

$("li.droppable").each(function(index) {
  var $this  = $(this);

  var header = $this.find("a.RootNode");
  var col    = $this.find("ul.ul-reset");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM