简体   繁体   中英

How can I retrieve the li tag of an unordered list and put these tags into an array using JQuery?

I am pretty new in JQuery and I have the following problem.

Into a page I have the following unordered list :

<ul id="menu-language" class="nav">
    <li id="menu-item-422" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-422">
        <a href="http://localhost/onyx/prova/">prova cat</a>
    </li>

    <li id="menu-item-185" class="qtranxs-lang-menu qtranxs-lang-menu-it menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-185">
        ..............................
        SOMETHING
        ..............................
    </li>
</ul>

I need to create a JQuery script that retrieve all the li tag (and the related HTML content of these tags) and that put it into something like an array (so each position of this array have to contain a li tag and its HTML content).

What is the smarter way to do it using JQuery?

Tnx

$('#menu-language > li') will return an array of li nodes, you can get the content of each li element by iterating:

$('#menu-language > li').each(function() {
console.log($(this).html());
});

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