开发者

Add list elements in DOM to array in JS

开发者 https://www.devze.com 2023-03-10 18:30 出处:网络
I have a un-ordered list containing links with the same class and a unique ID, I need to add all those ID\'s to an array in javascript with.

I have a un-ordered list containing links with the same class and a unique ID, I need to add all those ID's to an array in javascript with.

<ul>
<li><a class="item" id="10" href="#"></a></li>
<li><a class="item" id="11" href="#"></a></li>
<li><a class="item" id="12" href="#"></a></li>开发者_高级运维
<ul>

How could I do this?


var ids = document.getElements("ul li a.item").get("id");
console.log(ids); // ["10", "11", "12"]


an easier (more mootools-like) way to do what @Dimitar did:

var ids = $$('.item').get('id');

Good Luck

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号