(Javascript) document 文擋對象詳解

來源:互聯網
上載者:User

document 文擋對象 - JavaScript指令碼語言描述
---------------------------------------------------------------------
注:頁面上元素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           //設定字元集 簡體中文:gb2312
---------------------------------------------------------------------
對象方法
document.write()                  //動態向頁面寫入內容
document.createElement(Tag)       //建立一個html標籤對象
document.getElementById(ID)       //獲得指定ID值的對象
document.getElementsByName(Name)  //獲得指定Name值的對象
---------------------------------------------------------------------

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>

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

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.表單名.控制項名

-----------------------------------------------------------------------
<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>
-----------------------------------------------------------------------
<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 id="oDiv">Text</Div>
document.all.oDiv                       //引用圖層oDiv
document.all.oDiv.style                
document.all.oDiv.style.display=""      //圖層設定為可視
document.all.oDiv.style.display="none"  //圖層設定為隱藏
/*document.all表示document中所有對象的集合
只有ie支援此屬性,因此也用來判斷瀏覽器的種類*/

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.