document 文挡对象 - JavaScript脚本语言描述

来源:互联网 发布:如何装饰手机淘宝店铺 编辑:程序博客网 时间:2024/04/29 21:21

document 文挡对象 - JavaScript脚本语言描述

关键字:   document 文挡对象 - JavaScript脚本语言描述    

document 文挡对象 - JavaScript脚本语言描述
---------------------------------------------------------------------
注:页面上元素name属性和JavaScript引用的名称必须一致包括大小写
否则会提示你一个错误信息 "引用的元素为空或者不是对象"
---------------------------------------------------------------------
对象属性

代码
  1. document.title              //设置文档标题等价于HTML的<title>标签  
  2. document.bgColor            //设置页面背景色  
  3. document.fgColor            //设置前景色(文本颜色)  
  4. document.linkColor          //未点击过的链接颜色  
  5. document.alinkColor         //激活链接(焦点在此链接上)的颜色  
  6. document.vlinkColor         //已点击过的链接颜色  
  7. document.URL                //设置URL属性从而在同一窗口打开另一网页  
  8. document.fileCreatedDate    //文件建立日期,只读属性  
  9. document.fileModifiedDate   //文件修改日期,只读属性  
  10. document.fileSize           //文件大小,只读属性  
  11. document.cookie             //设置和读出cookie  
  12. document.charset            //设置字符集 简体中文:gb2312  

---------------------------------------------------------------------
常用对象方法
代码
  1. document.write()                   //动态向页面写入内容  
  2. document.createElement(Tag)        //创建一个html标签对象  
  3. document.getElementById(ID)        //获得指定ID值的对象  
  4. document.getElementsByName(Name)   //获得指定Name值的对象  
  5. document.body.appendChild(oTag)  

---------------------------------------------------------------------

body-主体子对象

代码
  1. document.body                //指定文档主体的开始和结束等价于<body></body>  
  2. document.body.bgColor        //设置或获取对象后面的背景颜色  
  3. document.body.link           //未点击过的链接颜色  
  4. document.body.alink          //激活链接(焦点在此链接上)的颜色  
  5. document.body.vlink          //已点击过的链接颜色  
  6. document.body.text           //文本色  
  7. document.body.innerText      //设置<body>...</body>之间的文本  
  8. document.body.innerHTML      //设置<body>...</body>之间的HTML代码  
  9. document.body.topMargin      //页面上边距  
  10. document.body.leftMargin     //页面左边距  
  11. document.body.rightMargin    //页面右边距  
  12. document.body.bottomMargin   //页面下边距  
  13. document.body.background     //背景图片  
  14.   
  15. document.body.appendChild(oTag) //动态生成一个HTML对象  

常用对象事件
代码
  1. document.body.onclick="func()"           //鼠标指针单击对象是触发  
  2. document.body.onmouseover="func()"       //鼠标指针移到对象时触发  
  3. document.body.onmouseout="func()"        //鼠标指针移出对象时触发   

---------------------------------------------------------------------
location-位置子对象
代码
  1. document.location.hash       // #号后的部分  
  2. document.location.host       // 域名+端口号  
  3. document.location.hostname   // 域名  
  4. document.location.href       // 完整URL  
  5. document.location.pathname   // 目录部分  
  6. document.location.port       // 端口号  
  7. document.location.protocol   // 网络协议(http:)  
  8. document.location.search     // ?号后的部分  
  9.   
  10. documeny.location.reload()       //刷新网页  
  11. document.location.reload(URL)    //打开新的网页  
  12. document.location.assign(URL)    //打开新的网页  
  13. document.location.replace(URL)   //打开新的网页  

---------------------------------------------------------------------
selection-选区子对象
document.selection
---------------------------------------------------------------------

images集合(页面中的图象)

a)通过集合引用

代码
  1. document.images              //对应页面上的<img>标签  
  2. document.images.length       //对应页面上<img>标签的个数  
  3. document.images[0]           //第1个<img>标签             
  4. document.images[i]           //第i-1个<img>标签  

b)通过nane属性直接引用

代码
  1. <img name="oImage">  
  2. document.images.oImage       //document.images.name属性  

c)引用图片的src属性

代码
  1. document.images.oImage.src   //document.images.name属性.src  

d)创建一个图象

代码
  1. var oImage  
  2. oImage = new Image()  
  3. document.images.oImage.src="1.jpg"  

同时在页面上建立一个<img>标签与之对应就可以显示
代码
  1. <html>  
  2. <img name=oImage>  
  3. <script language="javascript">  
  4.     var oImage  
  5.     oImage = new Image()  
  6.     document.images.oImage.src="1.jpg"  
  7. </script>  
  8. </html>  
  9.   
  10. <html>  
  11. <script language="javascript">  
  12.     oImage=document.caeateElement("IMG")  
  13.     oImage.src="1.jpg"  
  14.     document.body.appendChild(oImage)  
  15. </script>  
  16. </html>  

----------------------------------------------------------------------

forms集合(页面中的表单)

a)通过集合引用

代码
  1. document.forms                  //对应页面上的<form>标签  
  2. document.forms.length           //对应页面上<form>标签的个数  
  3. document.forms[0]               //第1个<form>标签  
  4. document.forms[i]               //第i-1个<form>标签  
  5. document.forms[i].length        //第i-1个<form>中的控件数  
  6. document.forms[i].elements[j]   //第i-1个<form>中第j-1个控件  

b)通过标签name属性直接引用
代码
  1. <form name="Myform"><input name="myctrl"></form>  
  2. document.Myform.myctrl          //document.表单名.控件名  

c)访问表单的属性
代码
  1. document.forms[i].name          //对应<form name>属性  
  2. document.forms[i].action        //对应<form action>属性  
  3. document.forms[i].encoding      //对应<form enctype>属性  
  4. document.forms[i].target        //对应<form target>属性  
  5.   
  6. document.forms[i].appendChild(oTag) //动态插入一个控件  

-----------------------------------------------------------------------
代码
  1. <html>  
  2. <!--Text控件相关Script-->  
  3. <form name="Myform">  
  4. <input type="text" name="oText">  
  5. <input type="password" name="oPswd">  
  6. <form>  
  7. <script language="javascript">  
  8. //获取文本密码框的值  
  9. document.write(document.Myform.oText.value)  
  10. document.write(document.Myform.oPswd.value)  
  11. </script>  
  12. </html>  
  13.   
  14. /////////////  
  15. <html>  
  16. <!--checkbox,radio控件相关script-->  
  17. <form name="Myform">  
  18. <input type="checkbox" name="chk" value="1">1       
  19. <input type="checkbox" name="chk" value="2">2       
  20. </form>       
  21. <script language="javascript">       
  22. function fun(){       
  23.    //遍历checkbox控件的值并判断是否选中       
  24.    var length       
  25.    length=document.forms[0].chk.length       
  26.    for(i=0;i<length;i++){       
  27.    v=document.forms[0].chk[i].value       
  28.    b=document.forms[0].chk[i].checked       
  29.    if(b)       
  30.      alert(vv=v+"被选中")       
  31.    else       
  32.      alert(vv=v+"未选中")      
  33.    }       
  34.    }       
  35. </script>        
  36. <a href=# onclick="fun()">ddd</a>                        
  37. </html>   
  38. -----------------------------------------------------------------------  
  39. <html>  
  40. <!--Select控件相关Script-->  
  41. <form name="Myform">  
  42. <select name="oSelect">  
  43. <option value="1">1</option>  
  44. <option value="2">2</option>  
  45. <option value="3">3</option>  
  46. </select>  
  47. </form>  
  48.   
  49. <script language="javascript">  
  50.     //遍历select控件的option项  
  51.     var length  
  52.     length=document.Myform.oSelect.length  
  53.     for(i=0;i<length;i++)  
  54.     document.write(document.Myform.oSelect[i].value)  
  55. </script>  
  56.   
  57. <script language="javascript">  
  58.     //遍历option项并且判断某个option是否被选中  
  59.     for(i=0;i<document.Myform.oSelect.length;i++){  
  60.     if(document.Myform.oSelect[i].selected!=true)  
  61.     document.write(document.Myform.oSelect[i].value)  
  62.     else  
  63.     document.write("<font color=red>"+document.Myform.oSelect[i].value+"</font>")     
  64.     }  
  65. </script>  
  66.   
  67. <script language="javascript">  
  68.     //根据SelectedIndex打印出选中的option  
  69.     //(0到document.Myform.oSelect.length-1)  
  70.     i=document.Myform.oSelect.selectedIndex  
  71.     document.write(document.Myform.oSelect[i].value)  
  72. </script>  
  73.   
  74. <script language="javascript">  
  75.     //动态增加select控件的option项  
  76.     var oOption = document.createElement("OPTION");  
  77.     oOption.text="4";  
  78.     oOption.value="4";  
  79.     document.Myform.oSelect.add(oOption);  
  80. </script>  
  81. <html>  

-----------------------------------------------------------------------
代码
  1. <Div id="oDiv">Text</Div>  
  2. document.all.oDiv                               //引用图层oDiv                  
  3. document.all.oDiv.style.display=""              //图层设置为可视  
  4. document.all.oDiv.style.display="none"          //图层设置为隐藏  
  5. document.getElementId("oDiv")                   //通过getElementId引用对象  
  6. document.getElementId("oDiv").style=""  
  7. document.getElementId("oDiv").display="none"  
  8. /*document.all表示document中所有对象的集合  
  9. 只有ie支持此属性,因此也用来判断浏览器的种类*/  

图层对象的4个属性
代码
  1. document.getElementById("ID").innerText   //动态输出文本  
  2. document.getElementById("ID").innerHTML   //动态输出HTML  
  3. document.getElementById("ID").outerText   //同innerText  
  4. document.getElementById("ID").outerHTML   //同innerHTML  
  5.   
  6. <html>  
  7. <script language="javascript">  
  8. function change(){  
  9. document.all.oDiv.style.display="none"  
  10. }  
  11. </script>  
  12. <Div id="oDiv" onclick="change()">Text</Div>  
  13. </html>  
  14.   
  15. <html>  
  16. <script language="javascript">  
  17. function changeText(){  
  18. document.getElementById("oDiv").innerText="NewText"  
  19. }  
  20. </script>  
  21. <Div id="oDiv" onmouseover="changeText()">Text</Div>  
  22. </html>