JavaScript中document對(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屬性從而在同一窗口打開(kāi)另一網(wǎng)頁(yè)
document.fileCreatedDate //文件建立日期,只讀屬性
document.fileModifiedDate //文件修改日期,只讀屬性
document.fileSize //文件大小,只讀屬性
document.cookie //設(shè)置和讀出cookie
document.charset //設(shè)置字符集 簡(jiǎn)體中文:gb2312
======================================================================
body-主體子對(duì)象
document.body //指定文檔主體的開(kāi)始和結(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)后的部分
常用對(duì)象事件
documeny.location.reload() //刷新網(wǎng)頁(yè)
document.location.reload(URL) //打開(kāi)新的網(wǎng)頁(yè)
document.location.assign(URL) //打開(kāi)新的網(wǎng)頁(yè)
document.location.replace(URL) //打開(kāi)新的網(wǎng)頁(yè)
======================================================================
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-1個(gè)<img>標(biāo)簽
b)通過(guò)name屬性直接引用
<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)就可以顯示
示例代碼(動(dòng)態(tài)創(chuà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-1個(gè)<form>標(biāo)簽
document.forms.length //第i-1個(gè)<form>中的控件數(shù)
document.forms.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.name //對(duì)應(yīng)<form name>屬性
document.forms.action //對(duì)應(yīng)<form action>屬性
document.forms.encoding //對(duì)應(yīng)<form enctype>屬性
document.forms.target //對(duì)應(yīng)<form target>屬性
document.forms.appendChild(oTag) //動(dòng)態(tài)插入一個(gè)控件
----------------------------
示例代碼(form):
<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>
----------------------------
示例代碼(checkbox):
<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.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控件相關(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.value)
</script>
<script language="javascript">
//遍歷option項(xiàng)并且判斷某個(gè)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">
//根據(jù)SelectedIndex打印出選中的option
//(0到document.Myform.oSelect.length-1)
i=document.Myform.oSelect.selectedIndex
document.write(document.Myform.oSelect.value)
</script>
<script language="javascript">
//動(dòng)態(tài)增加select控件的option項(xiàng)
var oOption = document.createElement("OPTION");
oOption.text="4";
oOption.value="4";
document.Myform.oSelect.add(oOption);
</script>
<html>
======================================================================
Div集合(頁(yè)面中的層)
<Div id="oDiv">Text</Div>
document.all.oDiv //引用圖層oDiv
document.all.oDiv.style.display="" //圖層設(shè)置為可視
document.all.oDiv.style.display="none" //圖層設(shè)置為隱藏
document.getElementId("oDiv") //通過(guò)getElementId引用對(duì)象
document.getElementId("oDiv").
document.getElementId("oDiv").display="none"
/*document.all表示document中所有對(duì)象的集合
只有ie支持此屬性,因此也用來(lái)判斷瀏覽器的種類*/
圖層對(duì)象的4個(gè)屬性
document.getElementById("ID").innerText //動(dòng)態(tài)輸出文本
document.getElementById("ID").innerHTML //動(dòng)態(tài)輸出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>
相關(guān)文章
Javascript基礎(chǔ)教程之關(guān)鍵字和保留字匯總
這篇文章主要介紹了Javascript基礎(chǔ)教程之關(guān)鍵字和保留字匯總,需要的朋友可以參考下2015-01-01JavaScript對(duì)內(nèi)存分配及管理機(jī)制詳細(xì)解析
本文主要講述了JavaScript的垃圾回收原理和具體的過(guò)程。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11JavaScript中的setMilliseconds()方法使用詳解
這篇文章主要介紹了JavaScript中的setMilliseconds()方法使用詳解,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06ECMAScript6的新特性箭頭函數(shù)(Arrow Function)詳細(xì)介紹
這篇文章主要介紹了ECMAScript6的新特性箭頭函數(shù)(Arrow Function)詳細(xì)介紹,ECMAScript6其實(shí)就是JavaScript,它的新特性就是JS的新特性,引入只是時(shí)間問(wèn)題,需要的朋友可以參考下2014-06-06js數(shù)組方法擴(kuò)展實(shí)現(xiàn)數(shù)組統(tǒng)計(jì)函數(shù)
這篇文章主要介紹了js數(shù)組方法擴(kuò)展,實(shí)現(xiàn)數(shù)組統(tǒng)計(jì)函數(shù),需要的朋友可以參考下2014-04-04JavaScript中用于四舍五入的Math.round()方法講解
這篇文章主要介紹了JavaScript中用于四舍五入的Math.round()方法講解,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06JavaScript sup方法入門實(shí)例(把字符串顯示為上標(biāo))
這篇文章主要介紹了JavaScript sup方法入門實(shí)例,sup方法用于把字符串顯示為上標(biāo),需要的朋友可以參考下2014-10-10