开发者

getElementById trouble

html: [...]<div id="test" name="testvalue"></div>[...]

js[...]alert(document.getElementById("test").name);[...]开发者_如何学运维

Why I get 'undefined' instead of 'testvalue'?


Because name is not a valid attribute for a <div> element. As such, it doesn't map to the .name property.

You could probably get it using the getAttribute() method though:

alert(document.getElementById("test").getAttribute("name"));

Example: http://jsfiddle.net/BcYXL/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜