[英]Fetching “id” from first 5 tags and separate them with comma in output using pure javascript
I am working on one of script where I need to pull the value available in id on tag I need to use pure Javascript to get this done. 我正在处理一种脚本,在其中我需要提取标记中id中可用的值,我需要使用纯Javascript来完成此操作。 I have similar jQuery code available but I am not able to successfully complete Javascript one.
我有类似的jQuery代码可用,但我无法成功完成Javascript。 My jquery Code
我的jQuery代码
var arr1st3 = $('article:lt(3)').map(function() {
return this.id.replace(/[^\d]/g, '');
}).get();
console.log( arr1st3 );
Here is the test url http://jsfiddle.net/0mvjbkhs/5/ 这是测试网址http://jsfiddle.net/0mvjbkhs/5/
I am trying to have Javascript function that will return the values in output like ['123456', '7896669', '1147777'] 我正在尝试使用Javascript函数,该函数将在输出中返回值,例如['123456','7896669','1147777']
Any help will be really appreciated 任何帮助将不胜感激
Pure javascript version would be: 纯JavaScript版本为:
var arr2st3 = [].map.call(document.querySelectorAll('article:nth-child(-n+4)'), function(el) {
return el.id.replace(/[^\d]/g, '');
});
Quite similar. 非常相似。 You just use native Array.prototype.map and
article:nth-child(-n+4)
CSS selector to select elements. 您只需使用本机Array.prototype.map和
article:nth-child(-n+4)
CSS选择器来选择元素。
Demo: http://jsfiddle.net/0mvjbkhs/6/ 演示: http : //jsfiddle.net/0mvjbkhs/6/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.