问题
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.com 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)]
回答1:
$('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.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>new</li>
<li>howler monkey</li>
<li>pine marten</li>
</ul>
回答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.com/toArray/
来源:https://stackoverflow.com/questions/45219858/queryselectorall-in-jquery