欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript中document對(duì)象使用詳解

 更新時(shí)間:2015年01月06日 12:03:55   投稿:hebedich  
這篇文章主要介紹了JavaScript中document對(duì)象使用詳解,總結(jié)的十分詳盡,需要的朋友可以參考下

對(duì)象屬性

復(fù)制代碼 代碼如下:

document.title                 //設(shè)置文檔標(biāo)題等價(jià)于HTML的<title>標(biāo)簽
document.bgColor               //設(shè)置頁(yè)面背景色
document.fgColor               //設(shè)置前景色(文本顏色)
document.linkColor             //未點(diǎn)擊過(guò)的鏈接顏色
document.alinkColor            //激活鏈接(焦點(diǎn)在此鏈接上)的顏色
document.vlinkColor            //已點(diǎn)擊過(guò)的鏈接顏色
document.URL                   //設(shè)置URL屬性從而在同一窗口打開(kāi)另一網(wǎng)頁(yè)
document.fileCreatedDate       //文件建立日期,只讀屬性
document.fileModifiedDate      //文件修改日期,只讀屬性
document.fileSize              //文件大小,只讀屬性
document.cookie                //設(shè)置和讀出cookie
document.charset               //設(shè)置字符集 簡(jiǎn)體中文:gb2312

======================================================================
body-主體子對(duì)象

復(fù)制代碼 代碼如下:

document.body                   //指定文檔主體的開(kāi)始和結(jié)束等價(jià)于<body></body>
document.body.bgColor           //設(shè)置或獲取對(duì)象后面的背景顏色
document.body.link              //未點(diǎn)擊過(guò)的鏈接顏色
document.body.alink             //激活鏈接(焦點(diǎn)在此鏈接上)的顏色
document.body.vlink             //已點(diǎn)擊過(guò)的鏈接顏色
document.body.text              //文本顏色
document.body.innerText         //設(shè)置<body>...</body>之間的文本
document.body.innerHTML         //設(shè)置<body>...</body>之間的HTML代碼
document.body.topMargin         //頁(yè)面上邊距
document.body.leftMargin        //頁(yè)面左邊距
document.body.rightMargin       //頁(yè)面右邊距
document.body.bottomMargin      //頁(yè)面下邊距
document.body.background        //背景圖片
document.body.appendChild(oTag) //動(dòng)態(tài)生成一個(gè)HTML對(duì)象

常用對(duì)象事件

復(fù)制代碼 代碼如下:

document.body.onclick="func()"              //鼠標(biāo)指針單擊對(duì)象是觸發(fā)
document.body.onmouseover="func()"          //鼠標(biāo)指針移到對(duì)象時(shí)觸發(fā)
document.body.onmouseout="func()"           //鼠標(biāo)指針移出對(duì)象時(shí)觸發(fā)

======================================================================
location-位置子對(duì)象

復(fù)制代碼 代碼如下:

document.location.hash          // #號(hào)后的部分
document.location.host          // 域名+端口號(hào)
document.location.hostname      // 域名
document.location.href          // 完整URL
document.location.pathname      // 目錄部分
document.location.port          // 端口號(hào)
document.location.protocol      // 網(wǎng)絡(luò)協(xié)議(http:)
document.location.search        // ?號(hào)后的部分

常用對(duì)象事件

復(fù)制代碼 代碼如下:

documeny.location.reload()          //刷新網(wǎng)頁(yè)
document.location.reload(URL)       //打開(kāi)新的網(wǎng)頁(yè)
document.location.assign(URL)       //打開(kāi)新的網(wǎng)頁(yè)
document.location.replace(URL)      //打開(kāi)新的網(wǎng)頁(yè)

======================================================================
images集合(頁(yè)面中的圖象)
a)通過(guò)集合引用

復(fù)制代碼 代碼如下:

document.images                 //對(duì)應(yīng)頁(yè)面上的<img>標(biāo)簽
document.images.length          //對(duì)應(yīng)頁(yè)面上<img>標(biāo)簽的個(gè)數(shù)
document.images[0]              //第1個(gè)<img>標(biāo)簽
document.images              //第i-1個(gè)<img>標(biāo)簽

b)通過(guò)name屬性直接引用

復(fù)制代碼 代碼如下:

<img name="oImage">
document.images.oImage          //document.images.name屬性

c)引用圖片的src屬性

復(fù)制代碼 代碼如下:

document.images.oImage.src      //document.images.name屬性.src

d)創(chuàng)建一個(gè)圖象

復(fù)制代碼 代碼如下:

var oImage
oImage = new Image()
document.images.oImage.src="1.jpg"

同時(shí)在頁(yè)面上建立一個(gè)<img>標(biāo)簽與之對(duì)應(yīng)就可以顯示
示例代碼(動(dòng)態(tài)創(chuàng)建圖象):

復(fù)制代碼 代碼如下:

<html>
<img name=oImage>
<script language="javascript">
       var oImage
       oImage = new Image()
       document.images.oImage.src="1.jpg"
</script>
</html>
<html>
<script language="javascript">
       oImage=document.caeateElement("IMG")
       oImage.src="1.jpg"
       document.body.appendChild(oImage)
</script>
</html>

=====================================================================
forms集合(頁(yè)面中的表單)
a)通過(guò)集合引用

復(fù)制代碼 代碼如下:

document.forms                     //對(duì)應(yīng)頁(yè)面上的<form>標(biāo)簽
document.forms.length              //對(duì)應(yīng)頁(yè)面上<form>標(biāo)簽的個(gè)數(shù)
document.forms[0]                  //第1個(gè)<form>標(biāo)簽
document.forms                  //第i-1個(gè)<form>標(biāo)簽
document.forms.length           //第i-1個(gè)<form>中的控件數(shù)
document.forms.elements[j]      //第i-1個(gè)<form>中第j-1個(gè)控件

----------------------------
b)通過(guò)標(biāo)簽name屬性直接引用

復(fù)制代碼 代碼如下:

<form name="Myform"><input name="myctrl"></form>
document.Myform.myctrl             //document.表單名.控件名

----------------------------
c)訪問(wèn)表單的屬性

復(fù)制代碼 代碼如下:

document.forms.name             //對(duì)應(yīng)<form name>屬性
document.forms.action           //對(duì)應(yīng)<form action>屬性
document.forms.encoding         //對(duì)應(yīng)<form enctype>屬性
document.forms.target           //對(duì)應(yīng)<form target>屬性
document.forms.appendChild(oTag) //動(dòng)態(tài)插入一個(gè)控件

----------------------------
示例代碼(form):

復(fù)制代碼 代碼如下:

<html>
<!--Text控件相關(guān)Script-->
<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>

----------------------------
示例代碼(checkbox):

復(fù)制代碼 代碼如下:

<html>
<!--checkbox,radio控件相關(guān)script-->
<form name="Myform">
<input type="checkbox" name="chk" value="1">1    
<input type="checkbox" name="chk" value="2">2    
</form>    
<script language="javascript">    
function fun(){    
      //遍歷checkbox控件的值并判斷是否選中    
      var length    
      length=document.forms[0].chk.length    
      for(i=0;i<length;i++){    
      v=document.forms[0].chk.value    
      b=document.forms[0].chk.checked    
      if(b)    
        alert(v=v+"被選中")    
      else    
        alert(v=v+"未選中")   
      }    
      }    
</script>     
<a href=# onclick="fun()">ddd</a>                     
</html>

----------------------------
示例代碼(Select):

復(fù)制代碼 代碼如下:

<html>
<!--Select控件相關(guān)Script-->
<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項(xiàng)
       var length
       length=document.Myform.oSelect.length
       for(i=0;i<length;i++)
       document.write(document.Myform.oSelect.value)
</script>
<script language="javascript">
       //遍歷option項(xiàng)并且判斷某個(gè)option是否被選中
       for(i=0;i<document.Myform.oSelect.length;i++){
       if(document.Myform.oSelect.selected!=true)
       document.write(document.Myform.oSelect.value)
       else
       document.write("<font color=red>"+document.Myform.oSelect.value+"</font>")  
       }
</script>
<script language="javascript">
       //根據(jù)SelectedIndex打印出選中的option
       //(0到document.Myform.oSelect.length-1)
       i=document.Myform.oSelect.selectedIndex
       document.write(document.Myform.oSelect.value)
</script>
<script language="javascript">
       //動(dòng)態(tài)增加select控件的option項(xiàng)
       var oOption = document.createElement("OPTION");
       oOption.text="4";
       oOption.value="4";
       document.Myform.oSelect.add(oOption);
</script>
<html>

======================================================================
Div集合(頁(yè)面中的層)

復(fù)制代碼 代碼如下:

<Div id="oDiv">Text</Div>
document.all.oDiv                                  //引用圖層oDiv               
document.all.oDiv.style.display=""                 //圖層設(shè)置為可視
document.all.oDiv.style.display="none"             //圖層設(shè)置為隱藏
document.getElementId("oDiv")                      //通過(guò)getElementId引用對(duì)象
document.getElementId("oDiv").
document.getElementId("oDiv").display="none"
/*document.all表示document中所有對(duì)象的集合
只有ie支持此屬性,因此也用來(lái)判斷瀏覽器的種類*/

圖層對(duì)象的4個(gè)屬性

復(fù)制代碼 代碼如下:

document.getElementById("ID").innerText      //動(dòng)態(tài)輸出文本
document.getElementById("ID").innerHTML      //動(dòng)態(tài)輸出HTML
document.getElementById("ID").outerText      //同innerText
document.getElementById("ID").outerHTML      //同innerHTML

----------------------------
示例代碼:

復(fù)制代碼 代碼如下:

<html>
<script language="javascript">
function change(){
document.all.oDiv.style.display="none"
}
</script>
<Div id="oDiv" onclick="change()">Text</Div>
</html>
<html>
<script language="javascript">
function changeText(){
document.getElementById("oDiv").innerText="NewText"
}
</script>
<Div id="oDiv" onmouseover="changeText()">Text</Div>
</html>

相關(guān)文章

最新評(píng)論