HTML DOM Document 对象

HTML DOM Document 对象

Document 对象

每个载入浏览器的 HTML 文档都会成为 Document 对象。

Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。

Document 对象集合

HTML DOM forms 集合

forms 集合可返回对文档中所有 Form 对象的引用。

document.forms[]实例

<html>
<body>
<form name="Form1"></form>
<form name="Form2"></form>
<form name="Form3"></form>
<script type="text/javascript">
document.write("This document contains: ")
document.write(document.forms.length + " forms.")
</script>
 </body>
 </html>

HTML DOM images 集合

images 集合可返回对文档中所有 Image 对象的引用。

实例

<body>
<img border="0" src="hackanm.gif" width="48" height="48">
<br />
<img border="0" src="compman.gif" width="107" height="98">
<br /><br />
<script type="text/javascript">
document.write("This document contains: ")
document.write(document.images.length + " images.")
</script>
</body>
</html>

HTML DOM cookie 属性

cookie 属性可设置或查询与当前文档相关的所有 cookie。

实例 The cookies associated with this document is:

HTML DOM domain 属性

domain 属性可返回下载当前文档的服务器域名。

实例

 <html>
<body>
 The domain name for this document is: 
<script type="text/javascript">
document.write(document.domain)
</script>
</body>
</html>

HTML DOM getElementById() 方法

getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用。

语法

document.getElementById(id)

HTML DOM getElementsByName() 方法

getElementsByName() 方法可返回带有指定名称的对象的集合。

语法

document.getElementsByName(name)

HTML DOM getElementsByTagName() 方法

getElementsByTagName() 方法可返回带有指定标签名的对象的集合。

语法

document.getElementsByTagName(tagname)

HTML DOM open() 方法

open() 方法可打开一个新文档,并擦除当前文档的内容。

语法 document.open(mimetype,replace)

mimetype 可选。规定正在写的文档的类型。默认值是 "text/html"。 
replace 可选。当此参数设置后,可引起新文档从父文档继承历史条目。 

重要事项:调用 open() 方法打开一个新文档并且用 write() 方法设置文档内容后,必须记住用 close 方法关闭文档,并迫使其内容显示出来。

注释:属于被覆盖的文档的一部分的脚本或事件句柄不能调用该方法,因为脚本或事件句柄自身也会被覆盖。

实例

<html>
<head>
<script type="text/javascript">
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="<html><body>Learning about the DOM is FUN!</body></html>";
newDoc.write(txt);
newDoc.close();
}
</script>
</head>
<body>
<input type="button" value="Write to a new document"
onclick="createNewDoc()">
</body>
</html>

HTML DOM close() 方法

close() 方法可关闭一个由 document.open 方法打开的输出流,并显示选定的数据。

语法 document.close()

说明

该方法将关闭 open() 方法打开的文档流,并强制地显示出所有缓存的输出内容。如果您使用 write() 方法动态地输出一个文档,必须记住当你这么做的时候要调用 close() 方法,以确保所有文档内容都能显示。

一旦调用了 close(),就不应该再次调用 write(),因为这会隐式地调用 open() 来擦除当前文档并开始一个新的文档。

实例

 <html>
 <head>
 <script type="text/javascript">
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="<html><body>Learning about the DOM is FUN!</body></html>";
 newDoc.write(txt);
newDoc.close();
}
</script>
</head>
<body>
<input type="button" value="Write to a new document"
onclick="createNewDoc()">
</body>
</html>

HTML DOM write() 方法

write() 方法可向文档写入 HTML 表达式或 JavaScript 代码。

可列出多个参数(exp1,exp2,exp3,...) ,它们将按顺序被追加到文档中。

语法 document.write(exp1,exp2,exp3,....)说明 虽然根据 DOM 标准,该方法只接受单个字符串作为参数。不过根据经验,write() 可接受任何多个参数。

我们通常按照两种的方式使用 write() 方法:一是在使用该方在文档中输出 HTML,另一种是在调用该方法的的窗口之外的窗口、框架中产生新文档。在第二种情况中,请务必使用 close() 方法来关闭文档。

实例

<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/rwalker/p/5495213.html