通过javascript删除html元素样式

dan*_*ods 85 html javascript dom styles

我正在尝试替换元素的内联样式标记值.当前元素如下所示:

`<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">`
Run Code Online (Sandbox Code Playgroud)

我想删除所有那些风格的东西,以便它的类型而不是它的内联样式.我试过删除element.style; 和element.style = null; 和element.style =""; 无济于事.我目前的代码在这些陈述中打破了.整个函数看起来像:
function unSetHighlight(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);


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

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

alert("unSet highlight called");
}
Run Code Online (Sandbox Code Playgroud)

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");
}
Run Code Online (Sandbox Code Playgroud)

clo*_*ead 168

你可以这样做:

element.removeAttribute("style")
Run Code Online (Sandbox Code Playgroud)

  • 或者`element.style.cssText = null`,它的功能大致相同. (44认同)
  • 这回答了OP的问题 - 删除所有内联样式和回退到样式表规则,但是......它也吹走了所有的内联样式.如果你想有选择地从内联样式中删除规则,@ sergio的答案(实际上,davidjb对该答案的评论)更有用. (5认同)
  • @mrec不完全相同,在你的情况下,一个元素仍然具有空的`style`属性 (2认同)

小智 67

在JavaScript中:

document.getElementById("id").style.display = null;
Run Code Online (Sandbox Code Playgroud)

在jQuery中:

$("#id").css('display',null);
Run Code Online (Sandbox Code Playgroud)

  • 第一行代码在Internet Explorer(<9)中出现错误,带有``Invalid argument``或导致元素在IE> = 9中完全消失.设置``getElementById("id").style.display = ''``,作为空字符串,似乎适用于浏览器. (13认同)
  • 在jQuery中删除样式的正确方法是`$("#id").css('display','');` (2认同)
  • display:与该问题或答案无关。那是为了隐藏元素。 (2认同)

Ron*_*ony 12

getElementById("id").removeAttribute("style");
Run Code Online (Sandbox Code Playgroud)

如果你正在使用jQuery那么

$("#id").removeClass("classname");
Run Code Online (Sandbox Code Playgroud)

  • 这两个代码片段做了截然不同的事情.只有第一个与问题有关. (2认同)

gap*_*ple 5

class属性可以包含多种样式,因此您可以将其指定为

<tr class="row-even highlight">
Run Code Online (Sandbox Code Playgroud)

并进行字符串操作以从element.className中删除“突出显示”

element.className=element.className.replace('hightlight','');
Run Code Online (Sandbox Code Playgroud)

使用jQuery会使方法更简单,因为您拥有方法

$("#id").addClass("highlight");
$("#id").removeClass("hightlight");
Run Code Online (Sandbox Code Playgroud)

这将使您轻松切换突出显示


Bal*_*aji 5

删除removeProperty

var el = document.getElementById("id");
el.style.removeProperty('display')

console.log("display removed" + el.style["display"])
console.log("color " + el.style["color"])
Run Code Online (Sandbox Code Playgroud)
<div id="id" style="display:block;color:red">s</div>
Run Code Online (Sandbox Code Playgroud)