HTML DOM学习之三

1.创建新的HTML元素:appendChild();

如需向HTML DOM添加元素,首先必须创建该元素,然后把它追加到已有的元素上;

<div id="div1">

<p id="p1">This is a paragraph.</p>

<p id="p2">This is another paragraph.</p>

</div>

<script>

var para=document.createElement("p");        //创建一个新的<p>元素

var node=document.createTextNode("This is new.");          //首先创建一个文本节点

para.appendChild(node);            //向<p>元素追加文本节点

var element=document.getElementById("div1");          //向查找到一个已有元素

element.appendChild(para);             //向这个已存在的元素追加新元素

</script>

2.创建新的HTML元素:insertBefore()方法;

<div id="div1">

<p id="p1">This is a paragraph.</p>

<p id="p2">This is another paragraph.</p>

</div>

<script>

var para=document.createElement("p");

var node=document.createTextNode("This is new.");

para.appendChild(node);

var element=document.getElementById("div1");

var child=document.getElementById("p1");

element.insertBefore(para,child);

</script>

3.删除已有的HTML元素:如需删除HTML元素,您必须清楚该元素的父元素

<div id="div1">

<p id="p1">This is a paragraph.</p>

<p id="p2">This is another paragraph.</p>

</div>

<script>

var parent=document.getElementById("div1");

var child=document.getElementById("p1");

parent.removeChild(child);     //从父元素中删除子元素

</script>

可以不查找父元素直接删除子元素的常用方法:

var child=document.getElementById("p1");

child.parentNode.removeChild(child);

4.替换HTML元素:如需替换HTML DOM中的元素,请使用replaceChild()方法:

<div id="div1">

<p id="p1">This is a paragraph.</p>

<p id="p2">This is another paragraph.</p>

</div>

<script>

var para=document.createElement("p");

var node=document.createTextNode("This is new .");

para.appendChild(node);

var parent=document.getElementById("div1");

var child=document.getElementById("p1");

parent.replaceChild(para,child);

</script>

5.HTML DOM-事件

**对事件作出反应:如需在用户点击某个元素时执行代码,请把JavaScript代码添加到HTML事件属性中:

<!DOCTYPE html>

<html>

<body>

<h1 onclick="this.innerHTML='Ooops!'">Chick on this text!</h1>

</body>

</html>

**HTML事件属性:如需向HTML元素分配事件,您可以使用事件属性:

<button onclick="displayDate()">Try it</button>

**使用HTML DOM来分配事件:

<script>

document.getElementById("myBtn").onclick=function(){displayDate()};

</script>

**onload和onunload事件

onload事件可用于检查访客的浏览器类型和版本,以便于基于这些信息加载不同版本的网页

onload和onunload事件可用于处理cookies

<body onload="checkCookies()">

**onchange事件:onchange事件常用于输入字段的验证

<input type="text" id="fname" onchange="upperCase()">

**onmouseover和onmouseout事件:用于在鼠标指针移动到或离开元素时触发函数

**onmousedown,onmouseup以及onclick事件:

onmousedown,onmouseup以及onclick事件是鼠标点击的全部过程,首先当鼠标被点击时,触发onmousedown事件,然后,当鼠标被松开时,会触发onmouseup事件,最后,当鼠标点击完成时,触发onclick事件;

6.HTML DOM导航:通过HTML DOM,能够使用节点关系在节点树中导航

**HTML DOM节点列表

var x=document.getElementsByTagName("p");   //选取文档中所有<p>节点

y=x[1];         //通过下标号访问这些节点,访问第二个<p>

**HTML DOM节点列表长度:length属性定义节点列表中节点的数量,可以使用length属性来循环节点列表:

x=document.getElementsByTagName("p");

for(i=0;i<x.length;i++){

document.write(x[i].innerHTML);

document.write("<br />")

}

**能够使用三个节点属性:parentNode,firstChild以及lastChild,在文档结构中进行导航

<html>

<body>                                            //<body>元素是首个<p>元素和<div>元素的父节点(parentNode)

<p id="intro">Hello World!</p>            //首个<p>元素是<body>元素的首个子元素(firstChild)

<div>                                               //<div>元素是<body>元素的最后一个子元素(lastChild)

  <p>The DOM is very useful!</p>

  <p>This example demonstrates node relationships.</p>

<script>

x=document.getElementById("intro");

document.write(x.firstChild.nodeValue);

</script>

</div>

</body>

</html>

**DOM根节点:这里有两个特殊的属性,可以访问全部文档:

document.documentElement-全部文档

document.body-文档主体

<html>

<body>

<p>Hello World!</p>

<div>

  <p>The DOM is very useful!</p>

  <p>This  example demonstrates the <b>document.body</b>property</p>

</div>

<script>

alert(document.body.innerHTML);

</script>

</body>

</html>

**ChildNodes和nodeValue:除了innerHTML属性,也可以使用childNodes和nodeValue属性来获取元素的内容;

<html>

<body>

<p id="intro">Hello World!</p>

<script>

var txt=document.getElementById("intro").childNodes[0].nodeValue;

document.write(txt);

</script>

</body>

</html>

原文地址:https://www.cnblogs.com/hqutcy/p/6010227.html