我正在尝试远程创建一个 onclick
对于每个 <div>
(以节省打字时间)。
这是 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 "game"
带我到网页"/games/undefined"
.
最佳答案
这会起作用。问题已得到纠正。
只需使用: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;}
}
}
关于javascript - this.name 在 javascript 中返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5528276/