开发者

getElementById trouble

开发者 https://www.devze.com 2023-03-17 04:58 出处:网络
html: [...]<div id=\"test\" name=\"testvalue\"></div>[...] js[...]alert(document.getElementById(\"test\").name);[...]开发者_如何学运维

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

精彩评论

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