js基础之DOM中document对象的常用属性方法详解
-----引入
每个载入浏览器的HTML文档都会成为Document对象。
Document对象使我们可以从脚本中对HTML页面中的所有元素进行访问。
属性
1 document.anchors 返回对文档中所有Anchor对象的引用。还有document.links/document.forms/document.images等
2 document.URL 返回当前文档的url
3 document.title 返回当前文档的标题
4 document.body 返回body元素节点
方法
1 document.close() 关闭用document.open()方法打开的输出流,并显示选定的数据。
<!DOCTYPEhtml>
<html>
<head>
<script>
functioncreateDoc()
{
varw=window.open();
w.document.open();
w.document.write("<h1>HelloWorld!</h1>");
w.document.close();
}
</script>
</head>
<body>
<inputtype="button"value="Newdocumentinanewwindow"onclick="createDoc()">
</body>
</html>
2 document.createElement() 创建元素节点。
<!DOCTYPEhtml>
<htmllang="en">
<!DOCTYPEhtml>
<head>
</head>
<body>
<p>helloworld</p>
<script>
vara=document.createElement('hr');
document.body.appendChild(a)
</script>
</body>
</html>
3 document.createAttribute() 创建属性节点。
<!DOCTYPEhtml>
<html>
<body>
<pid="demo">ClickthebuttontomakeaBUTTONelement.</p>
<buttononclick="myFunction()">Tryit</button>
<script>
functionmyFunction()
{
varbtn=document.createElement("BUTTON");
document.body.appendChild(btn);
};
</script>
</body>
</html>
4 document.createTextNode() 创建文本节点。
<!DOCTYPEhtml>
<htmllang="en">
<!DOCTYPEhtml>
<head>
</head>
<body>
<p>helloworld</p>
<script>
vara=document.createTextNode('hahahah');
document.body.appendChild(a)
</script>
</body>
</html>
5 document.getElementsByClassName() 返回文档中所有指定类名的元素集合,作为NodeList对象集合。所谓NodeList对象集合,是一个类似于数组的数据格式,仅仅提供了length属性,像数组中的push pop方法等都未提供。
6 document.getElementById()返回对拥有指定id的第一个对象的引用。
7 document.getElementsByName()返回带有指定名称的对象集合。
8 document.getElementsByTagName()返回带有指定标签名的对象集合。
9 document.write()向文档写HTML表达式或JavaScript代码。注意:当html文档加载完后再使用write方法,会导致write内容覆盖掉原本的html文档。
<!DOCTYPEhtml>
<htmllang="en">
<!DOCTYPEhtml>
<head>
</head>
<body>
<p>helloworld</p>
<script>
document.write('hahahh')
</script>
</body>
</html>
以上就是小编为大家带来的js基础之DOM中document对象的常用属性方法详解全部内容了,希望大家多多支持毛票票~