我正在尝试为每个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;} } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。