admin管理员组

文章数量:1415697

Like the title how can I use DOM querySelectorAll() by jQuery. I am using final version of jQuery and Chrome browser For example:

<ul>
    <li>new</li>
    <li>howler monkey</li>
    <li>pine marten</li>
</ul>

I tried $("li") but it just return the first li tag.

-P/S: I have the answer to resolve this issue. I download jquery.js from jquery and linked to it but it just return the first li tag.

$("li")
<li>​new​</li>​

Then I used cdn link, it returns array result for me. I don't know why.

$("li")
(3) [li, li, li#adorable, prevObject: r.fn.init(1)]

Like the title how can I use DOM querySelectorAll() by jQuery. I am using final version of jQuery and Chrome browser For example:

<ul>
    <li>new</li>
    <li>howler monkey</li>
    <li>pine marten</li>
</ul>

I tried $("li") but it just return the first li tag.

-P/S: I have the answer to resolve this issue. I download jquery.js from jquery. and linked to it but it just return the first li tag.

$("li")
<li>​new​</li>​

Then I used cdn link, it returns array result for me. I don't know why.

$("li")
(3) [li, li, li#adorable, prevObject: r.fn.init(1)]
Share Improve this question edited Jul 20, 2017 at 17:59 Death-is-the-real-truth 72.3k10 gold badges57 silver badges104 bronze badges asked Jul 20, 2017 at 16:01 Thai Hoc Ha NguyenThai Hoc Ha Nguyen 881 silver badge10 bronze badges 11
  • How tou tried $('li'),can you please post your effort – Death-is-the-real-truth Commented Jul 20, 2017 at 16:02
  • 1 A jQuery object pretty much is querySelectorAll(). I tried $("li") but it just return the first li tag. No it didn't. You probably attempted to use attr() on a collection and only got the first value back. We need to see the code which made you believe that $('li') only retrieved the first element. – Rory McCrossan Commented Jul 20, 2017 at 16:03
  • $("li") does not return just the first instance , it returns an array of all of them – Scott Selby Commented Jul 20, 2017 at 16:03
  • 2 UM, $ is in the console ain't jQuery, that is a shortcut for document.querySelector() developers.google./web/tools/chrome-devtools/console/… – epascarello Commented Jul 20, 2017 at 16:03
  • @AlivetoDie i tried too but it had the same result. – Thai Hoc Ha Nguyen Commented Jul 20, 2017 at 16:04
 |  Show 6 more ments

2 Answers 2

Reset to default 2

There is jQuery function to return the array of elements of your selector. "toArray", then for your code you can do:

var lis = $('li').toArray();

Your lis variable will be an array of elements found by your li selector. You can see more here https://api.jquery./toArray/

$('li') will give you all of li element objects.

Please Check below example:-

$(document).ready(function(){
  var data = [];
  $('li').each(function(){
    data.push($(this).text());
  });

  console.log(data);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li>new</li>
    <li>howler monkey</li>
    <li>pine marten</li>
</ul>

本文标签: javascriptquerySelectorAll in JqueryStack Overflow