通过 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 (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");
}
233407 次浏览

you can just do:

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

if you are using jQuery then

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

The class attribute can contain multiple styles, so you could specify it as

<tr class="row-even highlight">

and do string manipulation to remove 'highlight' from element.className

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

Using jQuery would make this simpler as you have the methods

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

that would enable you to toggle highlighting easily

In JavaScript:

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

In jQuery:

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

Use

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

For native javascript

In jQuery, you can use

$(".className").attr("style","");

Completly removing style, not only set to NULL

document.getElementById("id").removeAttribute("style")

Remove removeProperty

var el=document.getElementById("id");




el.style.removeProperty('display')


console.log("display removed"+el.style["display"])


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