日期:2014-05-16  浏览次数:20254 次

JavaScript BOM学习笔记6——document对象

? ? ? document对象实际上是window对象的属性。因此alert(window.document==document)将返回true。

? ? ? document对象的独特之处在于它既是BOM对象又是DOM对象。由于BOM没有可以指导实现的标准,因此每个浏览器实现的document都有不同。

? ? ? document对象有许多集合,提供对载入的页面的各个部位的访问,具体如下:

?

集合 说明
anchors 页面中所有锚的集合<a name="anchorname"></a>表示
applets 页面中所有applet集合
embeds 页面中所有嵌入对象集合<embed/>表示
forms 页面所有表单集合
images 页面所有图像集合
links

页面所有链接集合<a href=""></a>表示

? ? ? 可以通过数字(数组下标)或者名字引用document对象的每个集合。document.images[0]与document.images["imagename"]都是合法的。见下面的例子:

?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html lang="en">
<head>
    <title></title>
    <script type="text/javascript">
        function testDocument() {
            alert(document.links[0].href);
            alert(document.images["imgHome"]);
            alert(document.forms["frmSubscribe"]);
        }
    </script>
</head>
<body>
    <p>Welcome to my <a href="home.html">Home</a>away from home</P>
    <img src="home.jpg" align="right" name="imgHome"/>
    <form method="post" action="accept.cgi" name="frmSubscribe">
        <input type="text" name="textEmail"/>
        <input type="submit" value="Subscribe" onclick="testDocument()"/>
    </form>
</body>
</html>

?此外,document对象还有write()和writeln()方法,接收一个参数,即要写入文档的字符串。这两个方法都会将文本插入调用它们的地方。

?

?

参考书:
《JavaScript高级编程》Nicolas C. Zakas著, 曹力 张欣 等译。