this.name在javascript中返回undefined [英] this.name returns undefined in javascript

查看:77
本文介绍了this.name在javascript中返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为每个div远程创建一个onclick(以节省打字时间)。这是window.onload函数;

I am trying to remotely create an onclick for each div (to save typing time). Here is the window.onload function;

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>

The name of every div is "flyingsheep" - this value was set by the traditional < div name="flyingsheep" >

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

When I click the div, the iframe "game" takes me to the webpage "/games/undefined"

提前致谢。

(在Google Chrome和Fennec中测试过(我不知道最传统的浏览器))

(Tested in Google Chrome and Fennec (not the most conventional browsers, I know))

推荐答案

这将有效。问题已得到纠正。

This will work. the problem is corrected.

只需使用: 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;} 
        }
}

这篇关于this.name在javascript中返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆