JavaScript的document对象详解

来源:百度文库 编辑:神马文学网 时间:2024/05/18 22:55:37

JavaScript的document对象详解
1、对象属性
document.title //设置文档标题等价于HTML的标签<br>document.bgColor //设置页面背景色<br>document.fgColor //设置前景色(文本颜色)<br>document.linkColor //未点击过的链接颜色<br>document.alinkColor //激活链接(焦点在此链接上)的颜色<br>document.vlinkColor //已点击过的链接颜色<br>document.URL //设置URL属性从而在同一窗口打开另一网页<br>document.fileCreatedDate //文件建立日期,只读属性<br>document.fileModifiedDate //文件修改日期,只读属性<br>document.fileSize //文件大小,只读属性<br>document.cookie //设置和读出cookie<br>document.charset //设置字符集 简体中文:gb2312<br>---------------------------------------------------------------------</p><p>2、对象方法<br>document.write() //动态向页面写入内容<br>document.createElement(Tag) //创建一个html标签对象<br>document.getElementById(ID) //获得指定ID值的对象<br>document.getElementsByName(Name) //获得指定Name值的对象<br>---------------------------------------------------------------------</p><p>3、images集合(页面中的图象)</p><p>a)通过集合引用<br>document.images //对应页面上的<mip-img>标签<br>document.images.length //对应页面上<mip-img>标签的个数<br>document.images[0] //第1个<mip-img>标签 <br>document.images[i] //第i-1个<mip-img>标签</p><p>b)通过nane属性直接引用<br><mip-img name="oImage"><br>document.images.oImage //document.images.name属性</p><p>c)引用图片的src属性<br>document.images.oImage.src //document.images.name属性.src</p><p>d)创建一个图象<br>var oImage<br>oImage = new Image()<br>document.images.oImage.src="/1.jpg"<br>同时在页面上建立一个<mip-img>标签与之对应就可以显示</p><p><html><br><mip-img name=oImage><br><script language="javascript"><br>var oImage<br>oImage = new Image()<br>document.images.oImage.src="/1.jpg"<br></script><br></html></p><p>----------------------------------------------------------------------</p><p>4、forms集合(页面中的表单)</p><p>a)通过集合引用<br>document.forms //对应页面上的<form>标签<br>document.forms.length //对应页面上<form>标签的个数<br>document.forms[0] //第1个<form>标签<br>document.forms[i] //第i-1个<form>标签<br>document.forms[i].length //第i-1个<form>中的控件数<br>document.forms[i].elements[j] //第i-1个<form>中第j-1个控件</p><p>b)通过标签name属性直接引用<br><form name="Myform"><input name="myctrl"></form><br>document.Myform.myctrl //document.表单名.控件名</p><p>-----------------------------------------------------------------------<br><html><br><!--Text控件相关Script--><br><form name="Myform"><br><input type="text" name="oText"><br><input type="password" name="oPswd"><br><form><br><script language="javascript"><br>//获取文本密码框的值<br>document.write(document.Myform.oText.value)<br>document.write(document.Myform.oPswd.value)<br></script><br></html><br>-----------------------------------------------------------------------</p><p><html><br><!--Select控件相关Script--><br><form name="Myform"><br><select name="oSelect"><br><option value="1">1</option><br><option value="2">2</option><br><option value="3">3</option><br></select><br></form></p><p><script language="javascript"><br>//遍历select控件的option项<br>var length<br>length=document.Myform.oSelect.length<br>for(i=0;i<length;i++)<br>document.write(document.Myform.oSelect[i].value)<br></script></p><p><script language="javascript"><br>//遍历option项并且判断某个option是否被选中<br>for(i=0;i<document.Myform.oSelect.length;i++){<br>if(document.Myform.oSelect[i].selected!=true)<br>document.write(document.Myform.oSelect[i].value)<br>else<br>document.write("<font color=red>"+document.Myform.oSelect[i].value+"</font>") <br>}<br></script></p><p><script language="javascript"><br>//根据SelectedIndex打印出选中的option<br>//(0到document.Myform.oSelect.length-1)<br>i=document.Myform.oSelect.selectedIndex<br>document.write(document.Myform.oSelect[i].value)<br></script></p><p><script language="javascript"><br>//动态增加select控件的option项<br>var oOption = document.createElement("OPTION");<br>oOption.text="4";<br>oOption.value="4";<br>document.Myform.oSelect.add(oOption);<br></script><br><html><br>-----------------------------------------------------------------------</p><p><Div id="oDiv">Text</Div><br>document.all.oDiv //引用图层oDiv<br>document.all.oDiv.style <br>document.all.oDiv.style.display="" //图层设置为可视<br>document.all.oDiv.style.display="none" //图层设置为隐藏<br>/*document.all表示document中所有对象的集合<br>只有ie支持此属性,因此也用来判断浏览器的种类*/  <br> <br></p></div> <div class="zuoye-list-a"> <a href="/article/