主營(yíng)業(yè)務(wù):網(wǎng)站制作 網(wǎng)站優(yōu)化 小程序制作 石家莊網(wǎng)站制作網(wǎng)站建設(shè)有限公司歡迎您!
石家莊閃云網(wǎng)絡(luò)科技有限公司
客服熱線15383239821
如何讓自己的網(wǎng)站更有質(zhì)量?

[web編程]document對(duì)象詳解

發(fā)布時(shí)間:2010/8/3 13:56:56

[web編程]document對(duì)象詳解
document 文擋對(duì)象 - JavaScript腳本語(yǔ)言描述
---------------------------------------------------------------------
注:頁(yè)面上元素name屬性和JavaScript引用的名稱必須一致包括大小寫
否則會(huì)提示你一個(gè)錯(cuò)誤信息 "引用的元素為空或者不是對(duì)象"
---------------------------------------------------------------------
對(duì)象屬性
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屬性從而在同一窗口打開另一網(wǎng)頁(yè)
document.fileCreatedDate //文件建立日期,只讀屬性
document.fileModifiedDate //文件修改日期,只讀屬性
document.fileSize //文件大小,只讀屬性
document.cookie //設(shè)置和讀出cookie
document.charset //設(shè)置字符集 簡(jiǎn)體中文:gb2312
---------------------------------------------------------------------
常用對(duì)象方法
document.write() //動(dòng)態(tài)向頁(yè)面寫入內(nèi)容
document.createElement(Tag) //創(chuàng)建一個(gè)html標(biāo)簽對(duì)象
document.getElementById(ID) //獲得指定ID值的對(duì)象
document.getElementsByName(Name) //獲得指定Name值的對(duì)象
document.body.appendChild(oTag)
---------------------------------------------------------------------

body-主體子對(duì)象
document.body //指定文檔主體的開始和結(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ì)象事件
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ì)象

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)后的部分

documeny.location.reload() //刷新網(wǎng)頁(yè)
document.location.reload(URL) //打開新的網(wǎng)頁(yè)
document.location.assign(URL) //打開新的網(wǎng)頁(yè)
document.location.replace(URL) //打開新的網(wǎng)頁(yè)
---------------------------------------------------------------------
Selection-選區(qū)子對(duì)象
document.Selection
---------------------------------------------------------------------

images集合(頁(yè)面中的圖象)

a)通過(guò)集合引用
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] //第i-1個(gè)<img>標(biāo)簽

b)通過(guò)nane屬性直接引用
<img name="oImage">
document.images.oImage //document.images.name屬性

c)引用圖片的src屬性
document.images.oImage.src //document.images.name屬性.src

d)創(chuàng)建一個(gè)圖象
var oImage
oImage = new Image()
document.images.oImage.src="1.jpg"
同時(shí)在頁(yè)面上建立一個(gè)<img>標(biāo)簽與之對(duì)應(yīng)就可以顯示

<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ò)集合引用
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] //第i-1個(gè)<form>標(biāo)簽
document.forms[i].length //第i-1個(gè)<form>中的控件數(shù)
document.forms[i].elements[j] //第i-1個(gè)<form>中第j-1個(gè)控件

b)通過(guò)標(biāo)簽name屬性直接引用
<form name="Myform"><input name="myctrl"></form>
document.Myform.myctrl //document.表單名.控件名

c)訪問(wèn)表單的屬性
document.forms[i].name //對(duì)應(yīng)<form name>屬性
document.forms[i].action //對(duì)應(yīng)<form action>屬性
document.forms[i].encoding //對(duì)應(yīng)<form enctype>屬性
document.forms[i].target //對(duì)應(yīng)<form target>屬性

document.forms[i].appendChild(oTag) //動(dòng)態(tài)插入一個(gè)控件
-----------------------------------------------------------------------
<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>
-----------------------------------------------------------------------
<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[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控件相關(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[i].value)
</script>

<script language="javascript">
//遍歷option項(xiàng)并且判斷某個(gè)option是否被選中
for(i=0;i<document.Myform.oSelect.length;i++){
if(document.Myform.oSelect[i].Selected!=true)
document

相關(guān)新聞推薦

在線客服 : 服務(wù)熱線:15383239821 電子郵箱: 27535611@qq.com

公司地址:石家莊市新華區(qū)九中街江西大廈4062

備案號(hào):冀ICP備2022000585號(hào)-4