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)
小智 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)
Ron*_*ony 12
getElementById("id").removeAttribute("style");
Run Code Online (Sandbox Code Playgroud)
如果你正在使用jQuery那么
$("#id").removeClass("classname");
Run Code Online (Sandbox Code Playgroud)
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)
这将使您轻松切换突出显示
删除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)
| 归档时间: |
|
| 查看次数: |
181014 次 |
| 最近记录: |