Javascript获取动态设置数据-*属性

Javascript获取动态设置数据-*属性,javascript,html,Javascript,Html,假设我有多个类为game\u join\u a的元素,它们各自的data-tbl属性从数据库中动态设置。我想检索这些atribute,以便在代码中进一步使用它们 下面我使用的代码返回告诉我,a[I]是未定义的 a = document.getElementsByClassName("game_join_a"); for(i = 0; i < a.length; i++){ a[i].addEventListener("click", function(){ cons

假设我有多个类为
game\u join\u a
的元素,它们各自的
data-tbl
属性从数据库中动态设置。我想检索这些atribute,以便在代码中进一步使用它们

下面我使用的代码返回告诉我,
a[I]
是未定义的

a = document.getElementsByClassName("game_join_a");
for(i = 0; i < a.length; i++){
    a[i].addEventListener("click", function(){
        console.log(a[i].getAttribute("data-tbl"));
    });
}
a=document.getElementsByClassName(“游戏加入a”);
对于(i=0;i
试试这个

var a = document.getElementsByClassName("game_join_a");
for(i = 0; i < a.length; i++) {
    a[i].addEventListener("click", function (e) {
        console.log(e.currentTarget.getAttribute("data-tbl"));
    }, false);
}
var a=document.getElementsByClassName(“游戏加入a”);
对于(i=0;i
试试这个

var a = document.getElementsByClassName("game_join_a");
for(i = 0; i < a.length; i++) {
    a[i].addEventListener("click", function (e) {
        console.log(e.currentTarget.getAttribute("data-tbl"));
    }, false);
}
var a=document.getElementsByClassName(“游戏加入a”);
对于(i=0;i
试试这个

var a = document.getElementsByClassName("game_join_a");
for(i = 0; i < a.length; i++) {
    a[i].addEventListener("click", function (e) {
        console.log(e.currentTarget.getAttribute("data-tbl"));
    }, false);
}
var a=document.getElementsByClassName(“游戏加入a”);
对于(i=0;i
试试这个

var a = document.getElementsByClassName("game_join_a");
for(i = 0; i < a.length; i++) {
    a[i].addEventListener("click", function (e) {
        console.log(e.currentTarget.getAttribute("data-tbl"));
    }, false);
}
var a=document.getElementsByClassName(“游戏加入a”);
对于(i=0;i

尝试
console.log(this.getAttribute(“数据tbl”)好的,成功了。但是为什么
addEventListener
没有使用
this
?因为当绑定事件侦听器时,它位于for循环内,所以定义了[i]。触发单击事件时,未定义[i]。请尝试
console.log(this.getAttribute(“数据tbl”)好的,成功了。但是为什么
addEventListener
没有使用
this
?因为当绑定事件侦听器时,它位于for循环内,所以定义了[i]。触发单击事件时,未定义[i]。请尝试
console.log(this.getAttribute(“数据tbl”)好的,成功了。但是为什么
addEventListener
没有使用
this
?因为当绑定事件侦听器时,它位于for循环内,所以定义了[i]。触发单击事件时,未定义[i]。请尝试
console.log(this.getAttribute(“数据tbl”)好的,成功了。但是为什么
addEventListener
没有使用
this
?因为当绑定事件侦听器时,它位于for循环内,所以定义了[i]。当点击事件被触发时,[i]没有定义。这个答案没有错只是一种不同的方法这个答案没有错只是一种不同的方法这个答案没有错只是一种不同的方法这个答案没有错只是一种不同的方法这个答案没有错只是一种不同的方法请记住,
classList
属性不是由IE9和更低版本支持。。。请记住,
classList
属性不受IE9和更低版本的支持。。。请记住,
classList
属性不受IE9和更低版本的支持。。。请记住,
classList
属性不受IE9和更低版本的支持。。。