国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript中document對象使用詳解

2024-05-06 16:13:48
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了JavaScript中document對象使用詳解,總結的十分詳盡,需要的朋友可以參考下
 

對象屬性

 

復制代碼代碼如下:

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

 

======================================================================
body-主體子對象

 

復制代碼代碼如下:

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

 

常用對象事件

 

復制代碼代碼如下:

document.body.onclick="func()"              //鼠標指針單擊對象是觸發
document.body.onmouseover="func()"          //鼠標指針移到對象時觸發
document.body.onmouseout="func()"           //鼠標指針移出對象時觸發

 

======================================================================
location-位置子對象

 

復制代碼代碼如下:

document.location.hash          // #號后的部分
document.location.host          // 域名+端口號
document.location.hostname      // 域名
document.location.href          // 完整URL
document.location.pathname      // 目錄部分
document.location.port          // 端口號
document.location.protocol      // 網絡協議(http:)
document.location.search        // ?號后的部分

 

常用對象事件

 

復制代碼代碼如下:

documeny.location.reload()          //刷新網頁
document.location.reload(URL)       //打開新的網頁
document.location.assign(URL)       //打開新的網頁
document.location.replace(URL)      //打開新的網頁

 

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

 

復制代碼代碼如下:

document.images                 //對應頁面上的<img>標簽
document.images.length          //對應頁面上<img>標簽的個數
document.images[0]              //第1個<img>標簽
document.images              //第i-1個<img>標簽

 

b)通過name屬性直接引用

 

復制代碼代碼如下:

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

 

c)引用圖片的src屬性

 

復制代碼代碼如下:

document.images.oImage.src      //document.images.name屬性.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>
<html>
<script language="javascript">
       oImage=document.caeateElement("IMG")
       oImage.src="1.jpg"
       document.body.appendChild(oImage)
</script>
</html>

 

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

 

復制代碼代碼如下:

document.forms                     //對應頁面上的<form>標簽
document.forms.length              //對應頁面上<form>標簽的個數
document.forms[0]                  //第1個<form>標簽
document.forms                  //第i-1個<form>標簽
document.forms.length           //第i-1個<form>中的控件數
document.forms.elements[j]      //第i-1個<form>中第j-1個控件

 

----------------------------
b)通過標簽name屬性直接引用

 

復制代碼代碼如下:

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

 

----------------------------
c)訪問表單的屬性

 

復制代碼代碼如下:

document.forms.name             //對應<form name>屬性
document.forms.action           //對應<form action>屬性
document.forms.encoding         //對應<form enctype>屬性
document.forms.target           //對應<form target>屬性
document.forms.appendChild(oTag) //動態插入一個控件

 

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

 

復制代碼代碼如下:

<html>
<!--Text控件相關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):

 

復制代碼代碼如下:

<html>
<!--checkbox,radio控件相關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):

 

復制代碼代碼如下:

<html>
<!--Select控件相關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項
       var length
       length=document.Myform.oSelect.length
       for(i=0;i<length;i++)
       document.write(document.Myform.oSelect.value)
</script>
<script language="javascript">
       //遍歷option項并且判斷某個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">
       //根據SelectedIndex打印出選中的option
       //(0到document.Myform.oSelect.length-1)
       i=document.Myform.oSelect.selectedIndex
       document.write(document.Myform.oSelect.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集合(頁面中的層)

 

復制代碼代碼如下:

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

 

圖層對象的4個屬性

 

復制代碼代碼如下:

document.getElementById("ID").innerText      //動態輸出文本
document.getElementById("ID").innerHTML      //動態輸出HTML
document.getElementById("ID").outerText      //同innerText
document.getElementById("ID").outerHTML      //同innerHTML

 

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

 

復制代碼代碼如下:

<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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 大田县| 景宁| 天等县| 大城县| 莱阳市| 禹城市| 永州市| 祁连县| 榕江县| 泰兴市| 南开区| 密山市| 龙里县| 皋兰县| 民丰县| 城口县| 稻城县| 金塔县| 韶关市| 大姚县| 宁阳县| 永靖县| 临武县| 凤翔县| 肃宁县| 平顶山市| 永济市| 武鸣县| 抚远县| 山东省| 临沧市| 铜川市| 武宁县| 乳山市| 旬阳县| 古交市| 遂昌县| 莒南县| 利辛县| 邹平县| 嘉鱼县|