獲取和設置元素的內容:
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"; //操作子集
