DOM

来源:互联网 发布:java a星寻路算法 编辑:程序博客网 时间:2024/06/05 00:09

Node

nodeType和nodeName (nodeValue的值始终为null)

<html>    <body id="mybody">        <input id="btn" type="Button" value="click me" onclick="alert(this.value)" > </input>        <div id="mydiv" style="width:300px;height:300px;background-color:blue;text-align:center;margin:0 auto;">         </div>        <select> aaaaaaaaa        </select>        <input type="text">        </input>        <textarea> sasdasasd</textarea>    </body>    <script type="text/javascript">        var c = document.getElementById("mydiv");        alert(c);   // object HTMLDivElement        c.style.backgroundColor = '#ff0000';   // 代码修改正方形颜色        var b = document.getElementById("mybody");document.write("<strong>" + (new Date()).toString() + "</strong>");//      window.onload = function()     // onload 页面准备好后自动运行//      {//          document.write("Hello World");//      }        function show()   // 后面按钮点击的函数        {            alert("cao");        }        //show()if(c.nodeType == 1){    alert(c.nodeName);  // DIV}else{    alert("00");}        var firstChild = b.childNodes[0];        var secondChild = b.childNodes.item(1);        var count = b.childNodes.length;        alert(firstChild);  //object Text        alert(secondChild); //object HTMLDivElement        alert(count);  //  13var newNode = document.getElementById("btn"); //appendChild() 给childNodes末尾添加一个节点,或者移动一个元素//removeChild()//replaceChild()var z = b.appendChild(newNode);  //把按钮放到了最后alert(z == newNode);  // truealert(b.lastChild == newNode);  //truealert(b.childNodes.length);   // 13        var node = document.createElement("LI");  // 添加个元素        var textnode = document.createTextNode("Water");        var newNode1 = b.appendChild(node);        alert(newNode1 == node);  //true        alert(b.lastChild == node);  //true        alert(b.childNodes.length);  //14    </script></html>

Document

HTMLDocument的一个实例

document.title// 文档标题document.URL// 文档地址document.domain// 文档域名 三个中只有这个可设置,也只能设置为URL中包含的域document.referrer// 来源页面URL 
document.getElementById();document.getElementsByTagName();document.getElementsByName();// 返回带有给定name特性的所有元素,常用于单选按钮
document.write()  // 原样写入document.writeIn() // 字符串末尾加换行document.open()//   打开网页输出流document.close()

Element

<div id="mydiv" style="width:300px;height:300px;background-color:blue;text-align:center;margin:0 auto;"> alert(c.getAttribute("id"));    //mydivalert(c.getAttribute("class"));  //nullalert(c.getAttribute("title"));//nullalert(c.getAttribute("lang"));//nullalert(c.getAttribute("dir"));//null// 还可以取自定义的特性     // style和 onclick 不能取  c.setAttribute("class","ft");  // 设置或替换特性alert(c.getAttribute("class"));   // ftc.removeAttribute("class");   //删除特性alert(c.getAttribute("class")); //nulldocument.createElement();// 创建元素

Comment

注释

document.createComment(); // 创建注释

CDATASection

0 0
原创粉丝点击