通过JavaScript移除HTML元素样式

22 浏览
0 Comments

通过JavaScript移除HTML元素样式

我正在尝试替换一个元素内联样式标签的值。当前元素如下所示:

``

我想要移除所有这些样式,以便通过类名来进行样式设置而不是通过内联样式。我尝试过`delete 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");
}

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