簡體   English   中英

通過javascript刪除html元素樣式

[英]removing html element styles via 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">`

並且我想刪除所有樣式的東西,以便它由它的類而不是它的內聯樣式來設置樣式。 我試過刪除 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");
}

你可以這樣做:

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

並進行字符串操作以從 element.className 中刪除“highlight”

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

使用 jQuery 會使這更簡單,因為您擁有這些方法

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

這將使您能夠輕松切換突出顯示

采用

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

對於原生 javascript

在 jQuery 中,您可以使用

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

完全刪除樣式,不僅設置為NULL

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

移除 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM