2009-06-24 127 views
47

我試圖替換元素的內嵌樣式標記值。當前元素看起來是這樣的:通過javascript刪除html元素樣式

`<tr class="row-even" style="background: red none repeat scroll 0% 0%; position: relative; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="0000ph2009-06-10s1s02">` 

,我想除去所有風格的東西,所以,它的通過它的類風格,而不是它的內嵌樣式。我試過刪除element.style;和element.style = null;和element.style =「」;無濟於事。我目前的代碼違反了這些聲明。整個函數看起來像:
功能unSetHighlight(指數){

if(index < 10) 
index = "000" + (index); 
else if (index < 100) 
    index = "000" + (index); 
    else if(index < 1000) 
    index = "0" + (index); 
    if(index >= 1000) 
     index = index; 

var mainElm = document.getElementById('active_playlist'); 
var elmIndex = ""; 

for(var currElm = mainElm.firstChild; currElm !== null; currElm = currElm.nextSibling){ 
    if(currElm.nodeType === 1){ 

    var elementId = currElm.getAttribute("id"); 

    if(elementId.match(/\b\d{4}/)){ 

    elmIndex = elementId.substr(0,4); 


    if(elmIndex == index){ 
     var that = currElm; 
     //that.style.background = position: relative; 
     } 
    } 
    } 
} 

clearInterval(highlight); 
alert("cleared Interval"); 
that.style.background = null; 

alert("unSet highlight called"); 
} 

調用clearInterval工作,但警告從未火災和背景保持不變。任何人看到任何問題?在此先感謝...


function unSetHighlight(index){ 
    alert(index); 
    if(index < 10) 
    index = "000" + (index); 
    else if (index < 100) 
     index = "000" + (index); 
     else if(index < 1000) 
     index = "0" + (index); 
     if(index >= 1000) 
      index = index; 

    var mainElm = document.getElementById('active_playlist'); 
    var elmIndex = ""; 

    for(var currElm = mainElm.firstChild; currElm !== null; currElm = currElm.nextSibling){ 
     if(currElm.nodeType === 1){ 

     var elementId = currElm.getAttribute("id"); 

     if(elementId.match(/\b\d{4}/)){ 

     elmIndex = elementId.substr(0,4); 
     alert("elmIndex = " + elmIndex + "index = " + index); 


     if(elmIndex === index){ 
      var that = currElm; 
      alert("match found"); 
      } 
     } 
     } 
    } 

    clearInterval(highlight); 
    alert("cleared Interval"); 
    that.removeAttribute("style"); 

    //that.style.position = "relative"; 
    //reColor(); 
    alert("unSet highlight called"); 
} 
+0

的removeAttribute試過( 「風格」),仍然沒有運氣。我使用setInterval來循環背景顏色來(嘗試)創建一個脈衝效果。我會嘗試寫一些其他風格類來嘗試答案4.任何其他想法?我當前的代碼發佈如下... – danwoods 2009-06-24 20:53:17

+0

如果您接受[this](http://stackoverflow.com/a/1040439/2008111)作爲此問題的正確答案,那將是非常棒的 – caramba 2017-02-13 12:01:07

+0

^這不是正確的答案,但是。 – 2017-06-23 10:13:23

回答

106

,你可以這樣做:element.removeAttribute("style")

+3

優雅的MAX – sidonaldson 2013-09-11 16:32:35

+26

或`element.style.cssText = null`,它的功能大致相同。 – mrec 2014-03-19 19:17:17

11
getElementById("id").removeAttribute("style"); 

,如果你使用jQuery然後

$("#id").removeClass("classname"); 
4

class屬性可以包含多個款式,所以你可以指定它爲

<tr class="row-even highlight"> 

做字符串操作從element.className

element.className=element.className.replace('hightlight',''); 

使用jQuery你有方法

$("#id").addClass("highlight"); 
$("#id").removeClass("hightlight"); 

,這將使你會作出這種簡單的刪除 '亮點'輕鬆切換突出顯示

44

在JavaScript中:

document.getElementById("id").style.display = null; 

在jQuery中:

$("#id").css('display',null); 
1

使用

particular_node.classList.remove("<name-of-class>")

對於本地JavaScript