我正在尝试为每个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;} } }