2009-06-24 131 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