简体   繁体   English

如何获取所有LI UL元素ID值并将它们放置在JavaScript数组中?

[英]How do I get all the LI UL elements ID value and place them in a JavaScript array?

I have to embed some tracking code on my site. 我必须在网站上嵌入一些跟踪代码。 So I have a list of LI elements with an ID value that I want to place inside an array of the snippet. 因此,我有一个LI元素的列表,这些元素具有ID值,我想将其放置在代码段数组中。 They should be numeric like, 123, 456, etc inside an object. 它们应为数字,例如对象内部的123、456等。 I want to do it in pure JavaScript. 我想用纯JavaScript做到这一点。

This is my code I have tried. 这是我尝试过的代码。 My HTML: 我的HTML:

<ul id="itemGrid">  
    <li class="item" id="1080">  product code </li>
    <li class="item" id="1487">  product code </li>    
    <li class="item" id="1488">  product code </li>
    ...
</ul>

This is the JavaScript code 这是JavaScript代码

// Get all LI items and get the ID of them in the object viewList
var catId = document.getElementById('itemGrid').getElementsByTagName('li');

window.criteo_q = window.criteo_q || [];
    window.criteo_q.push(
        // SHOULD BE LIKE THIS
        // { event: "viewList", item: ["First item id", "Second item id", "Third item id"] }
        // My actual code
        { event: "viewList", item: [ catId[].id ] }
);

try this 尝试这个

var lis = document.getElementById('itemGrid').getElementsByTagName('li');
var idArray = [];
for ( var counter = 0; counter < lis.length; counter++)
{
   idArray.push( lis[ counter ].id );
}
console.log( idArray );

You can use querySelectorAll to select all the matching elements passed as selector. 您可以使用querySelectorAll选择所有作为选择器传递的匹配元素。

The selector '#itemGrid li[id]' will select all the <li> elements inside #itemGrid element having id attribute on it. 选择器'#itemGrid li[id]'将选择#itemGrid元素中具有id属性的所有<li>元素。

The querySelectorAll returns a collection of HTML elements. querySelectorAll返回HTML元素的集合。 Iterate over this collection to get the individual element id. 遍历此集合以获取单个元素ID。

 var lis = document.querySelectorAll('#itemGrid li[id]'); var arr = []; for (var i = 0; i < lis.length; i++) { arr.push(+lis[i].id); } console.log(arr); document.write('<pre>' + JSON.stringify(arr, 0, 4) + '</pre>'); 
 <ul id="itemGrid"> <li class="item" id="1080">1080</li> <li class="item" id="1487">1487</li> <li class="item" id="1488">1488</li> </ul> <hr /> 

You can convert your HTMLCollection to an Array by passing it through slice , and you can then map that array: 您可以通过将slice传递给HTMLCollection ,将其转换为Array,然后可以map该数组:

catId = Array.prototype.slice.call(catId).map(function(li) { return li.id; });

 var catId = document.getElementById('itemGrid').getElementsByTagName('li'); catId = Array.prototype.slice.call(catId).map(function(li) { return li.id; }); document.write(catId); 
 <ul id="itemGrid"> <li class="item" id="1080"> product code </li> <li class="item" id="1487"> product code </li> <li class="item" id="1488"> product code </li> </ul> 

var lis = document.getElementById('itemGrid').getElementsByTagName('li');
var arr = [];

// You need to iterate the list lis
[].forEach.call( lis, function(el){
     arr.push( el.id );
});

// Making the object you want
var criteo_q = { event: "viewList", item: arr };
console.log(criteo_q);

To iterate the list of DOM elements, you can also use 要迭代DOM元素list ,您还可以使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在特定“ul”元素下获取多个“li”元素并将它们放入数组中? - How would I get multiple "li" elements under a specific "ul" element and put them into an array? 如何获得所有可见的“标题”属性 <li> specefic中的元素 <ul> 使用jquery,在数组中收集主题并使用alert()显示它们; - how to get “title” attribute of all visible <li> elements in specefic <ul> using jquery, collect theme in an array and display them using alert(); 当我尝试对它们进行排序时,如何在javascript中获取当前ul li的长度? - How to get the current ul li's length in javascript , when i tried to sort them? 如何将具有特定css样式的所有li元素移动到另一个ul? - How do I move all the li elements with specific css style to another ul? 如何在javascript中的&#39;ul&#39;中获取标签&#39;li&#39;的子元素 - How To Get Child Elements Of tag 'li' In The 'ul' In javascript 如何根据单击的ul id从json获取数据? - How do i get data from json based on which ul li id is clicked? 如何关闭同级 LI Elements 的 UL? - How do I close the UL of sibling LI Elements? 我该如何过滤 <li> 在一个 <ul> 有特定的ID? - How do I filter <li> under a<ul> having a particular id? 如何使用Javascript在li里面的ul里面获取div里面的值? - How to get value inside div inside ul inside li with Javascript? 如何在使用jquery或javascript进行悬停时显示此ul li下拉菜单? - How do I get this ul li dropdown menu to show on hover with jquery or javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM