Ответ 1
Вы можете просто сделать:
element.removeAttribute("style")
Я пытаюсь заменить значение тега inline style. Текущий элемент выглядит следующим образом:
`<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");
}
Вы можете просто сделать:
element.removeAttribute("style")
В JavaScript:
document.getElementById("id").style.display = null;
В jQuery:
$("#id").css('display',null);
getElementById("id").removeAttribute("style");
если вы используете jQuery, тогда
$("#id").removeClass("classname");
Атрибут class может содержать несколько стилей, поэтому вы можете указать его как
<tr class="row-even highlight">
и выполните строковые манипуляции, чтобы удалить "highlight" из element.className
element.className=element.className.replace('hightlight','');
Использование jQuery сделает это проще, поскольку у вас есть методы
$("#id").addClass("highlight");
$("#id").removeClass("hightlight");
который позволит вам легко переключаться
Использование
particular_node.classList.remove("<name-of-class>")
Для встроенного javascript
В jQuery вы можете использовать
$ ( "Имя класса ") атр (" стиль", "");