1.document属性
- cookie -- 用户cookie
- title -- 当前页面title标签中定义的文字
- URL -- 当前页面的URL
document代表HTML文档的内容,因此可以通过它表示文档中加载的一些元素,这些元素全部通过集合访问。
- anchors -- 文档中所有锚(a name="aname")的集合
- applets -- 文档中所有applet标签表示的内容的集合
- embeds -- 文档中所有embed标签表示的内容的集合
- forms -- 文档中所有form标签表示的内容的集合
- images -- 文档中所有image标签表示的内容的集合
- links -- 文档中所有a(链接)标签表示的内容的集合
2.document函数
- JavaScript write() 函数
- JavaScript writeln() 函数
- JavaScript document.open() 函数
- JavaScript document.close() 函数
a. document.write -- 在文档中写入字符串
write函数语法
document.write(str);
write函数参数
-
str -- 要写入文档中的字符串
b.document.writeln -- 在文档中写入字符串,并在字符串的末尾增加一个换行符
c.document.open -- 打开已经载入的文档
示例
var win = window.open("about:blank","dreamdu");
win.document.open();
win.document.write("welcome to dreamdu!");
win.document.close();
首先新建一个空白文档,并打开open,写入内容,最后完成显示关闭文档close。
document.open函数语法
window.document.open();
d.document.close -- 用于关闭document.open方法打开的文档
document.close函数语window.document.close();
3.使用document索引页面内的元素
可以使用数字或名称索引页面中的元素集合,每个元素的属性都变成了集合中相应对象的属性。
示例
<form name="form1"><a href="http://www.dreamdu.com/xhtml/" name="a1">xhtml</a></form>
<form name="form2"><a href="http://www.dreamdu.com/css/" name="a2">css</a></form>
<form name="form3"><a href="http://www.dreamdu.com/javascript/" name="a3">javascript</a></form>
<input type="button" value="显示第二个表单的名称" onclick="alert(document.forms[1].name)" />
<input type="button" value="显示第二个表单的名称第二种方法" onclick="alert(document.forms['form2'].name)" />
<input type="button" value="显示第三个链接的名称" onclick="alert(document.links[2].name)" />
<input type="button" value="显示第三个链接的名称第二种方法" onclick="alert(document.links['a3'].name)" />
<input type="button" value="显示第三个链接href属性的值" onclick="alert(document.links[2].href)" />
表示第二个表单的方法:document.forms[1]或document.forms["form2"]
表示第三个链接的方法:document.links[2]或document.links["a3"]
表示第三个链接href属性的方法:document.links[2].href