this.name在javascript中返回undefined

前端之家收集整理的这篇文章主要介绍了this.name在javascript中返回undefined前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为每个div远程创建一个onclick(以节省打字时间).这是window.onload函数;
window.onload = function() {
            divel = document.getElementsByTagName('div');
            for(var el in divel){
                divel[el].onmouSEOver = function(){ this.style.textDecoration = "underline"; };
                divel[el].onmouSEOut = function(){ this.style.textDecoration = "none"; };
                divel[el].onclick = function(){ document.getElementById('game').src = "/games/" + this.name; };
            }
}

每个div的名称是“flyingsheep” – 这个值是由传统的< div name =“flyingsheep”>

当我点击div时,iframe“游戏”将我带到网页“/ games / undefined”

提前致谢.

(在谷歌浏览器和Fennec中测试过(不是最传统的浏览器,我知道))

解决方法

这会奏效.问题得到纠正.

只需使用:this.attributes [“name”].value

window.onload = function() { 
        divel = document.getElementsByTagName('div');
        for(var el in divel){
        window.alert(divel[el].name);
            divel[el].onmouSEOver = function(){ this.style.textDecoration = "underline"; };
            divel[el].onmouSEOut = function(){ this.style.textDecoration = "none"; };
            divel[el].onclick = function(){document.getElementById('game').src = this.attributes["name"].value;} 
        }
}
原文链接:https://www.f2er.com/js/154803.html

猜你在找的JavaScript相关文章