document代表HTML文档的内容,因此可以通过它表示文档中加载的一些元素,这些元素全部通过集合访问。
a. document.write -- 在文档中写入字符串
document.write(str);
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。
window.document.open();
d.document.close -- 用于关闭document.open方法打开的文档
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
原文:http://www.cnblogs.com/qinxuemei/p/3970453.html