在网页中,我有一些代码,如
<span commandArgument="a">A</span>
<span commandArgument="b">B</span>
我想使用javascript来获取跨度的commandArgument值,我写的代码是
var spans = document.getElementsByTagName("span");
var value = spans[0].commandArgument;
它适用于IE但在Firefox中失败 IE获取值=“a”,Firefox获得值=未定义 有没有任何方法可以获得在两个浏览器中都有效的值?谢谢!
答案 0 :(得分:1)
您是否尝试过.getAttribute("commandArgument")
?
......必须提到丑陋的堂兄,.setAttribute("commandArgument", "c")
答案 1 :(得分:1)
您可以使用getAttribute()
中的methods of the DOM element。
根据quirskmode.org的compatibility sheet
,此方法也适用于旧浏览器var value = spans[0].getAttribute('commandArgument');