javascript擷取和設定元素的屬性

來源:互聯網
上載者:User

標籤:type   click   classname   www.   block   width   css   自訂屬性   put   

擷取和設定元素的內容:

var nv = document.getElementById("pid");  

alert(nv.innerHTML);  

nv.innerHTML="<a href=‘http://www.sina.com‘>到新浪</a>";    //瀏覽器會將inneHTML後面的內容作為html來解析

nv.innerText="World";    //瀏覽器會將innerText後面的內容作為純文字來解析

<input type="button" value="value"onclick="show(this)"></input>

nv.value="world";  //value是元素的屬性值,而innerText和innerHTML是元素開始和結束標籤之間的值。

 

自訂屬性:

window.onload=function(){

  var oA = document.getElementsByTagName("a");

  alert(oA[0].href);  //只能擷取到元素中內建的屬性

  alert(oA[0].getAttribute("width"));  //getAttribute() 能擷取到元素的所有屬性

}

dom操作css:

window.onload=function(){

   var oBtn1 = document.getElementById("btn1");

  var oDiv = document.getElementsByTagName("div")[0];

  var oA = document.getElementsByTagName("a")[0];

  oBtn1.onclick=function(){

    oDiv.style.width="200px";

    oDiv.className="current";

    oA.style.display="block";  

  }

}

this.parentNode.style.display="none";  //操作父級

this.children[1].style.display="none";  //操作子集

javascript擷取和設定元素的屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.