[英]Get "id" values from first three tags only and separate them with "," in output
[英]Fetching “id” from first 5 tags and separate them with comma in output using pure javascript
我正在處理一種腳本,在其中我需要提取標記中id中可用的值,我需要使用純Javascript來完成此操作。 我有類似的jQuery代碼可用,但我無法成功完成Javascript。 我的jQuery代碼
var arr1st3 = $('article:lt(3)').map(function() {
return this.id.replace(/[^\d]/g, '');
}).get();
console.log( arr1st3 );
這是測試網址http://jsfiddle.net/0mvjbkhs/5/
我正在嘗試使用Javascript函數,該函數將在輸出中返回值,例如['123456','7896669','1147777']
任何幫助將不勝感激
純JavaScript版本為:
var arr2st3 = [].map.call(document.querySelectorAll('article:nth-child(-n+4)'), function(el) {
return el.id.replace(/[^\d]/g, '');
});
非常相似。 您只需使用本機Array.prototype.map和article:nth-child(-n+4)
CSS選擇器來選擇元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.