详细讲解JavaScript脚本语言的 document 对象
http://www.webjx.com 更新日期:2007-10-04 23:00 出处:网页教学网 作者:整理
对象属性
代码
- document.title
- document.bgColor
- document.fgColor
- document.linkColor
- document.alinkColor
- document.vlinkColor
- document.URL
- document.fileCreatedDate
- document.fileModifiedDate
- document.fileSize
- document.cookie
- document.charset
对象方法
代码
- document.write()
- document.createElement(Tag)
- document.getElementById(ID)
- document.getElementsByName(Name)
images集合(页面中的图象)
a)通过集合引用
代码
- document.images
- document.images.length
- document.images[0]
- document.images[i]
b)通过nane属性直接引用
代码
- <img name="oImage">
- document.images.oImage
c)引用图片的src属性
代码
- document.images.oImage.src
d)创建一个图象
代码
- var oImage
- oImage = new Image()
- document.images.oImage.src="/1.jpg"
同时在页面上建立一个<img>标签与之对应就可以显示
代码
- <html>
- <img name=oImage>
- <script language="javascript">
- var oImage
- oImage = new Image()
- document.images.oImage.src="/1.jpg"
- </script>
- </html>
forms集合(页面中的表单)
a)通过集合引用
代码
- document.forms
- document.forms.length
- document.forms[0]
- document.forms[i]
- document.forms[i].length
- document.forms[i].elements[j]
b)通过标签name属性直接引用
代码
- <form name="Myform"><input name="myctrl"></form>
- document.Myform.myctrl //document.表单名.控件名
代码
- <html>
-
- <form name="Myform">
- <input type="text" name="oText">
- <input type="password" name="oPswd">
- <form>
- <script language="javascript">
- //获取文本密码框的值
- document.write(document.Myform.oText.value)
- document.write(document.Myform.oPswd.value)
- </script>
- </html>
代码
- <html>
-
- <form name="Myform">
- <select name="oSelect">
- <option value="1">1</option>
- <option value="2">2</option>
- <option value="3">3</option>
- </select>
- </form>
-
- <script language="javascript">
- //遍历select控件的option项
- var length
- length=document.Myform.oSelect.length
- for(i=0;i<length;i++)
- document.write(document.Myform.oSelect[i].value)
- </script>
-
- <script language="javascript">
- //遍历option项并且判断某个option是否被选中
- for(i=0;i<document.Myform.oSelect.length;i++){
- if(document.Myform.oSelect[i].selected!=true)
- document.write(document.Myform.oSelect[i].value)
- else
- document.write("<font color=red>"+document.Myform.oSelect[i].value+"</font>")
- }
- </script>
-
- <script language="javascript">
- //根据SelectedIndex打印出选中的option
- //(0到document.Myform.oSelect.length-1)
- i=document.Myform.oSelect.selectedIndex
- document.write(document.Myform.oSelect[i].value)
- </script>
-
- <script language="javascript">
- //动态增加select控件的option项
- var oOption = document.createElement("OPTION");
- oOption.text="4";
- oOption.value="4";
- document.Myform.oSelect.add(oOption);
- </script>
- <html>
代码
- <Div id="oDiv">Text</Div>
- document.all.oDiv //引用图层oDiv
- document.all.oDiv.style
- document.all.oDiv.style.display="" //图层设置为可视
- document.all.oDiv.style.display="none" //图层设置为隐藏
/*document.all表示document中所有对象的集合
只有ie支持此属性,因此也用来判断浏览器的种类*/
关键词:JavaScript,document,对象
【推荐给好友】【关闭】【收藏本文】
您的评论
·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为