2009-06-24 14 views
47

Próbuję zastąpić wartość znacznika stylu elementu. Obecny elementem wygląda następująco:Usuwanie stylów elementów html za pośrednictwem javascript

`<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">` 

i chciałbym, aby usunąć wszystkie te rzeczy tak, że styl to stylizowany przez swojej klasie zamiast to styl inline. Próbowałem usunąć element.style; i element.style = null; i element.style = ""; bez skutku. Mój bieżący kod łamie się przy tych stwierdzeniach. Cała funkcja wygląda następująco:
funkcji unSetHighlight (indeksu) {

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 działa, ale nieczysto nigdy pożary, a tło pozostaje taka sama. Ktoś widzi jakieś problemy? Z góry dzięki ...


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

Tried removeAttribute ("styl"), wciąż bez powodzenia. Używam setInterval do przełączania kolorów tła, aby (spróbować) stworzyć efekt pulsu. Spróbuję napisać kilka innych klas stylu, aby wypróbować odpowiedź 4. Jakieś inne pomysły? Mój aktualny kod jest zamieszczony poniżej ... – danwoods

+0

Byłoby wspaniale, gdybyś zaakceptował [to] (http://stackoverflow.com/a/1040439/2008111) jako poprawną odpowiedź na to pytanie. – caramba

+0

^To nie jest właściwa odpowiedź , chociaż. –

Odpowiedz

106

można po prostu zrobić: element.removeAttribute("style")

+3

elegancki do MAX – sidonaldson

+26

Lub 'element.style.cssText = null', który robi to samo. – mrec

+1

@mrec nie jest dokładnie taki sam, w twoim przypadku element nadal ma pusty atrybut "style" – user

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

jeśli używasz jQuery następnie

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

Te dwa fragmenty kodu robią szalenie różne rzeczy. Tylko pierwszy ma coś wspólnego z pytaniem. – JasonWoof

+0

@JasonWoof Zgadzam się. –

4

atrybut klasa może zawierać wiele stylów, więc możesz go określić jako

<tr class="row-even highlight"> 

i zrobić manipulacji ciąg, aby usunąć 'highlight' z element.className

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

jQuery byłoby to prostsze, jak masz metody

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

które pozwalają do łatwego przełączania podświetlania

+0

Kolejną zaletą definiowania .highlight w twoim arkuszu stylów jest to, że możesz dokładnie zmienić "highlight", zmieniając tylko jedną linię CSS i nie wprowadzając żadnych zmian w JavaScript. Jeśli nie używasz JQuery, dodawanie i usuwanie klasy jest nadal bardzo proste:/* on */theElement.className + = 'highlight ";/* off */theElement.className = theElement.className.replace (/ \ b \ s * highlight \ b /, ''); (Definiując .element w CSS, automatycznie pozostaje on także wszędzie). –

+0

Ups, zapomniałem klasy możliwości podano więcej niż jeden raz. Aby obsłużyć tę sprawę, należy dodać flagę "g" do wyrażenia regularnego: theElement.className = theElement.className.replace (/ \/b \ s * highlight \ b/g, ''); –

+0

użyj właściwości węzła classlist zamiast className –

44

W JavaScript:

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

w jQuery:

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

Pierwsza linia kodu pojawia się albo w programie Internet Explorer (<9) z błędem' ' Niepoprawny argument'' lub spowodować, że element zniknie całkowicie w IE> = 9. Ustawienie '' getElementById ("id"). Style.display = '' '', będący pustym łańcuchem, wydaje się działać w różnych przeglądarkach. – davidjb

+2

w jQuery poprawnym sposobem usunięcia stylu jest '$ (" # id "). Css ('display', '');' –

+0

To zbliżyło mnie do siebie, ale nie było zerowe, ale "brak" działało np '$ (" # id "). css (wyświetlanie", "brak"); " – Aaron

1

Zastosowanie

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

dla rodzimych javascript

Powiązane problemy