[web編程]document對象詳解
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值的對象
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) //動態插入一個控件
-----------------------------------------------------------------------
<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>
<!--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>
-----------------------------------------------------------------------
<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
?石家莊網站建設的優點:我們是專屬定制,制作符合您公司業務、風格的網站,這就做到給您制作的網站是網絡上獨一無二的網站,這樣更有利于搜索引擎的收錄。如果您喜歡網絡上某個風格的網站或者網站模板,我們是只仿制而不直接套用,我們會對其中的結構和樣式做增減優化,做…
?競價排名營銷是一種通過 競價購買搜索引擎中關鍵詞或關鍵短語的廣告方式。競價排名營銷分為兩種類型。 第一種,直接為真正的搜索引擎結果的排名付費,也就是說付的錢越多,網頁所獲得的排名名也就越高。 第二種則更類似于普通的廣告。這種競價排名營銷方式通過…
?網站優化對網站來說是非常重要的,直接決定和影響著網站所起到的作用,以及在各個地方當中的排名。在進行seo優化的過程中,我們必須要堅持一定的原則,真正的把這些事情做得更好,才可以確保最終的結果,這對于我們來說是非常重要的事。 SEO優化主要分為8小步: …