
我们这节课来说document对象。
Document对象代表了当前的HTML文档,它的作用跟window对象同样重要。
通过document对象我们可以访问页面中的各种元素并可以改变它们的状态,并且我们可以在文档的任意位置使用这个对象。
一、document对象的属性
下面我把document对象的各种属性列出来:
1、alinkcolor:激活的链接颜色
2、anchors:书签
3、cookie:就是cookie,不多解释
4、fgcolor:前景色
5、forms:文档中的表单
6、lastModified:文档的最后修改时间
7、linkcolor:链接的颜色
8、links:本页面中的链接,数组形式
9、location:本文档的URL
10、referrer:返回前一个URL
11、title:本文档标题
12、vlinkcolor:访问过的链接颜色
我们在前边的课程中讲过,在面向对象的编程里,如果我们想访问一个对象的属性,一般都是这样写:
对象名.属性名
在这样同样是这样,比如我可以这样使用document对象的属性:
Document.linkcolor="red"
通过这样的语句,我就可以在页面中动态的改变页面的链接颜色了。
二、document对象的方法
1、open方法
用来打开一个文档,执行该方法后,当前文档内的所有内容被清除,可以使用write或writeLn方法将新内容写入文档。
2、write方法
用于向文档中写入HTML代码
3、writeLn方法
同样用于向文档中写入HTML代码,但与write方法不同的是,writeln方法会在写完一句HTML代码后加上一个换行。
4、close方法
用来关闭当前文档
5、clear方法
用来清除当前文档的内容
现在我们来写一个脚本文件,来使用上边的知识点:
------------------------vbs_24.htm------------------------
<html>
<head>
<title>VBS系列课程--Document对象应用示例</title>
<script language="vbscript">
dim a,b
a=document.linkcolor
b=document.bgcolor
sub button1_onclick
document.linkcolor="red"
end sub
sub button2_onclick
document.linkcolor=a
end sub
sub button3_onclick
document.bgcolor="teal"
end sub
sub button4_onclick
document.bgcolor=b
end sub
sub fortext
form1.text1.value=document.title
form1.text2.value=document.location
end sub
</script>
</head>
<body onload="fortext">
<form name="form1">
单击这里通过document对象的linkcolor属性来改变当前文档中的链接颜**r>
<br>
<input type="button" name="button1" value="单击这里改变">
<input type="button" name="button2" value="单击这里还原">
<br>
<br>
<a href="http://tieba.baidu.com/罗小叉">点击此处访问百度罗小叉吧!</a>
<br>
<br>
单击这里通过document对象的bgcolor属性来改变当前文档中的背景颜**r>
<br>
<input type="button" name="button3" value="单击这里改变">
<input type="button" name="button4" value="单击这里还原">
<br>
<br>
本文标题:
<input type="text" name="text1" size="40">
本文地址:
<input type="text" name="text2" size="40">
<br>
<br>
<hr align="left" color="red" size="2" width="80%">
<br>
<br>
本文最新更新日期:
<script language="vbscript">
document.write(document.lastmodified)
</script>
</form>
</body>
</html>
来看看效果:
Document对象代表了当前的HTML文档,它的作用跟window对象同样重要。
通过document对象我们可以访问页面中的各种元素并可以改变它们的状态,并且我们可以在文档的任意位置使用这个对象。
一、document对象的属性
下面我把document对象的各种属性列出来:
1、alinkcolor:激活的链接颜色
2、anchors:书签
3、cookie:就是cookie,不多解释
4、fgcolor:前景色
5、forms:文档中的表单
6、lastModified:文档的最后修改时间
7、linkcolor:链接的颜色
8、links:本页面中的链接,数组形式
9、location:本文档的URL
10、referrer:返回前一个URL
11、title:本文档标题
12、vlinkcolor:访问过的链接颜色
我们在前边的课程中讲过,在面向对象的编程里,如果我们想访问一个对象的属性,一般都是这样写:
对象名.属性名
在这样同样是这样,比如我可以这样使用document对象的属性:
Document.linkcolor="red"
通过这样的语句,我就可以在页面中动态的改变页面的链接颜色了。
二、document对象的方法
1、open方法
用来打开一个文档,执行该方法后,当前文档内的所有内容被清除,可以使用write或writeLn方法将新内容写入文档。
2、write方法
用于向文档中写入HTML代码
3、writeLn方法
同样用于向文档中写入HTML代码,但与write方法不同的是,writeln方法会在写完一句HTML代码后加上一个换行。
4、close方法
用来关闭当前文档
5、clear方法
用来清除当前文档的内容
现在我们来写一个脚本文件,来使用上边的知识点:
------------------------vbs_24.htm------------------------
<html>
<head>
<title>VBS系列课程--Document对象应用示例</title>
<script language="vbscript">
dim a,b
a=document.linkcolor
b=document.bgcolor
sub button1_onclick
document.linkcolor="red"
end sub
sub button2_onclick
document.linkcolor=a
end sub
sub button3_onclick
document.bgcolor="teal"
end sub
sub button4_onclick
document.bgcolor=b
end sub
sub fortext
form1.text1.value=document.title
form1.text2.value=document.location
end sub
</script>
</head>
<body onload="fortext">
<form name="form1">
单击这里通过document对象的linkcolor属性来改变当前文档中的链接颜**r>
<br>
<input type="button" name="button1" value="单击这里改变">
<input type="button" name="button2" value="单击这里还原">
<br>
<br>
<a href="http://tieba.baidu.com/罗小叉">点击此处访问百度罗小叉吧!</a>
<br>
<br>
单击这里通过document对象的bgcolor属性来改变当前文档中的背景颜**r>
<br>
<input type="button" name="button3" value="单击这里改变">
<input type="button" name="button4" value="单击这里还原">
<br>
<br>
本文标题:
<input type="text" name="text1" size="40">
本文地址:
<input type="text" name="text2" size="40">
<br>
<br>
<hr align="left" color="red" size="2" width="80%">
<br>
<br>
本文最新更新日期:
<script language="vbscript">
document.write(document.lastmodified)
</script>
</form>
</body>
</html>
来看看效果:
