[英]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.