querySelectorAll in Jquery

限于喜欢 提交于 2021-02-05 11:48:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!