2010-05-17 203 views
0

我想在javascript中使用下面的语句去除DOM元素的属性。removeAttribute不返回true或false

var res = nodes.removeAttribute("style"); 

但资源永远是“不确定”似乎的removeAttribute函数不返回任何东西(我测试了Firefox浏览器)

我如何确定该属性被成功删除或不?

感谢, Sourabh

+1

http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-removeAttribute - “这个方法返回什么。” – bobince 2010-05-17 14:05:14

回答

2

nodes?看起来你有一组node s。无论如何,removeAttribute不会返回任何东西。要检查属性是否已被删除,请使用hasAttribute

node.removeAttribute('foo'); 
if (node.hasAttribute('foo')) { 
    // failed, foo still exists. 
} 
0

nodes.removeAttribute( “风格”);

var isTheLanguageWorkingLikeItShouldAndThisCheckIsTotalallyPointless = nodes.getAttribute(“style”);

+0

如果我不什么在删除它之前知道样式attr是否已经存在? – Sourabh 2010-05-17 13:25:51

+0

然后在“删除”之前执行“获取”。 – 2010-05-17 13:40:12

+0

这将是太多了...但似乎它是唯一的方法来做到这一点。 – Sourabh 2010-05-17 13:42:01