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/
精彩评论