I am trying to get all possible data-* attributes from every tag on my page.
var el = document.body.querySelectorAll("*");
for(i=0;i<el.length;i++){
var datas = [].filter.call(el[i].attributes, function(at) { return /^data-/.test(at.name); });
}
now datas variable contains attribute objects and I want to search datas.name for specific value, in my case I'm searching for "data-val". But typeof datas.name returns undefined. Any clue what to do? Pure JavaScript please. Thanx!
update
I could go with the following solution:
var el = document.body.querySelectorAll("*");
for(i=0;i<el.length;i++){
var datas = [].filter.call(el[i].attributes, function(at) { return /^data-/.test(at.name); });
for(var k=0;k<datas.length;k++){
nodes.push(datas[k].name.replace(/^data-/,""));
}
for(var l=0;l<nodes.length;l++) {
if(nodes.search("val")>0){
...do my code here...
}
}
}
but is there a more elegant way to do that?
var el = document.body.querySelectorAll("*"),
i = 0,
j = 0,
len = el.length;
for (; i < len; i++) {
var x = el[i].attributes,
len2 = x.length;
for (; j < len2; j++) {
if (x[j].name === "data-val") console.log("data-val present");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.