javascript - 通过 javascript 删除 html 元素样式

标签 javascript html 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">`

而且我想删除所有样式内容,以便按类而不是内联样式设置样式。我试过 delete 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");
}

最佳答案

你可以这样做:

element.removeAttribute("style")

关于javascript - 通过 javascript 删除 html 元素样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1040402/

相关文章:

javascript - React什么时候调用组件构造函数以及执行流程是怎样的?

html - 在 Mobile Chrome 的 iframe 中显示 PDF,而不是下载到设备

html - 如何在 img 之上堆叠图像?

html - Div 彼此不对齐

javascript - 将内容添加到列表时保持滚动位置 (AngularJS)

javascript - Firefox 或 JavaScript,计算 DOM

javascript - 获取嵌套数组项以附加到 HTML 表中的新列

javascript - AngularJS 更改集合项之间的 View

javascript - DataTable 中的 Actionbutton 不起作用,Shiny DT

javascript - 将 JSON 中的大数字解析为字符串