1. 程式人生 > >JavaScript的document物件詳解

JavaScript的document物件詳解

 [注:頁面上元素name屬性和JavaScript引用的名稱必須一致包括大小寫,否則會提示你一個錯誤資訊 "引用的元素為空或者不是物件"]

========================================================================
物件屬性:
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           //設定字符集 <strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="_u7B80_u4F53_u4E2D_u6587";KeyGate_ads.ShowGgAds(this,"_u7B80_u4F53_u4E2D_u6587",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://www.google.com/aclk?sa=L&ai=BWB1ZM8mLR_vHD5Hs6APo0pX0Crv0hjCXu4LqAuaDwXmwvRAQBhgGIK-aqgkoBzgBUM-CrjxgncnggegFqgEKMTAwMDAxNjAwMsgBAakCEqvo4uaBgj7IAuenlgLZAymfZiyPFXMt&num=6&q=http://www.showtec.cn/products/drylabel.html&sig=AGiWqtyKbVAB7I92GDPHMy8GClqLRDwKbw");GgKwClickStat("簡體中文","www.showtec.cn","afs","1000016002");" onmouseout="isShowGg = false;InTextAds_GgLayer="_u7B80_u4F53_u4E2D_u6587"">簡體中文:gb2312
----------------------------
常用物件方法:
document.write()                  //動態
向頁面寫入內容 document.createElement(Tag) //建立一個html標籤物件 document.getElementById(ID) //獲得指定ID值的物件 document.getElementsByName(Name) //獲得指定Name值的物件 document.body.appendChild(oTag) ======================================================================== 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) //開啟新的網頁 ======================================================================== selection-選區子物件 document.selection ======================================================================== images集合(頁面中的圖象): ---------------------------- a)通過集合引用 document.images //對應頁面上的<img>標籤 document.images.length //對應頁面上<img>標籤的個數 document.images[0] //第1個<img>標籤 document.images[i] //第i-1個<img>標籤 ---------------------------- b)通過nane屬性直接引用 <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] //第i-1個<form>標籤 document.forms[i].length //第i-1個<form>中的控制元件數 document.forms[i].elements[j] //第i-1個<form>中第j-1個控制元件 ---------------------------- b)通過標籤name屬性直接引用 <form name="Myform"><input name="myctrl"></form> document.Myform.myctrl //document.表單名.控制元件名 ---------------------------- c)訪問表單的屬性 document.forms[i].name //對應<form name>屬性 document.forms[i].action //對應<form action>屬性 document.forms[i].encoding //對應<form enctype>屬性 document.forms[i].target //對應<form target>屬性 document.forms[i].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[i].value b=document.forms[0].chk[i].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[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集合(頁面中的層): <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").style="" 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>