![](/img/trans.png)
[英]How to add a new attribute for a HTML element before added to DOM with jQuery
[英]How to select a DOM element with a new attribute added by JavaScript?
附:
var i = 0;
jQuery("a").each(function(){
this.nthcounter = i;
i += 1;
});
我本來希望
jQuery("a[nthcounter=20]").click();
要么
document.querySelector("a[nthcounter=20]").click();
上班,但似乎他們什么都沒做。
嘗試這個:
var i = 0;
jQuery("a").each(function(){
$(this).attr("nthcounter", i++);
});
您正在向DOM對象本身添加屬性...而不是通過jQuery API添加屬性。
要執行我認為您要嘗試執行的操作,請執行以下操作:
var i = 0;
jQuery("a").each(function(){
// jQuery object attr...
$(this).attr("nthcounter", i);
// Increment
i++;
});
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.