js | javascript获取和设置元素的属性

获取和设置元素的内容:

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() 能获取到元素的所有属性

  oA.setAttribute("tittle","a lot of goods") //建立一个属性,并同时给属性捆绑一个值

  createAttribute:仅建立一个属性
  removeAttribute:删除一个属性
  getAttributeNode:获取一个节点作为对象
  setAttributeNode:建立一个节点
  removeAttributeNode:删除一个节点

}

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";  //操作子集

原文地址:https://www.cnblogs.com/guanzelin/p/8794917.html