我試圖替換元素的內嵌樣式標記值。當前元素看起來是這樣的:通過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");
}
的removeAttribute試過( 「風格」),仍然沒有運氣。我使用setInterval來循環背景顏色來(嘗試)創建一個脈衝效果。我會嘗試寫一些其他風格類來嘗試答案4.任何其他想法?我當前的代碼發佈如下... – danwoods 2009-06-24 20:53:17
如果您接受[this](http://stackoverflow.com/a/1040439/2008111)作爲此問題的正確答案,那將是非常棒的 – caramba 2017-02-13 12:01:07
^這不是正確的答案,但是。 – 2017-06-23 10:13:23