2010-05-17 196 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