DOM == Document Object Model
查找元素:
var x=document.getElementById("main");
var y=x.getElementsByTagName("p");(查找id为main的元素下的所有p元素)
var z=document.getElementsByClassName("container");(查找id为main的元素下所有class为container的元素)
通过类名查找HTML元素在ie5、6、7、8中无效
改变html元素内容:document.getElementById("content").innerHTML = "newText"
改变html元素属性:document.getElementById("img").src = "qingdao.jpg"
改变html元素样式:document.getElementById("p1").style.color = "blue"
常用的事件类型有:
一个鼠标移入移出的事件例子:onmouseover="style.color='red'" onmouseout="style.color='blue'"
添加节点的步骤:
var strong = document.createElement("strong");var txt = document.createTextNode("这是新添加的文本节点; ");strong.appendChild(txt);document.getElementById("addHtmlNode").appendChild(strong);删除节点的步骤:
var strong = document.getElementsByClassName("willBeRm");(生成元素时已添加此类名)var parent = document.getElementById("addHtmlNode");if(strong[0]) parent.removeChild(strong[0]);if(strong[0]) strong[0].parentNode.removeChild(strong[0]);