簡體   English   中英

Javascript單擊多個元素上的事件偵聽器並獲取目標“ rel”屬性

[英]Javascript click event listener on multiple elements and get target 'rel' attribute

碼:

var articles = document.getElementsByTagName('li');
for (var i = 0; i < articles.length; i++) {
    articles[i].addEventListener('click',redirect,false);
}
function redirect(){
    alert(this.rel);
}

此代碼適用於屬性ID,但不適用於屬性rel。 誰能幫我獲得rel屬性?

您應該使用element.getAttribute('attribute_name')訪問這些屬性element.getAttribute('attribute_name')

alert(this.getAttribute('rel'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM