DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> JS的Document屬性和方法小結
JS的Document屬性和方法小結
編輯:JavaScript基礎知識     
document.title //設置文檔標題等價於HTML的title標簽
document.bgColor //設置頁面背景色
document.fgColor //設置前景色(文本顏色)
document.linkColor //未點擊過的鏈接顏色
document.alinkColor //激活鏈接(焦點在此鏈接上)的顏色
document.vlinkColor //已點擊過的鏈接顏色
document.URL //設置URL屬性從而在同一窗口打開另一網頁
document.fileCreatedDate //文件建立日期,只讀屬性
document.fileModifiedDate //文件修改日期,只讀屬性
document.charset //設置字符集 簡體中文:gb2312
document.fileSize //文件大小,只讀屬性
document.cookie //設置和讀出cookie

———————————————————————
常用對象方法

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 // 域名+端口號//好像返回的是主機名localhost,沒有返回端口號
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

例如:
復制代碼 代碼如下:
<div>請選中這裡的部分文字。</div>
<div><input type="button" value="請選中部分文字,然後點擊這裡執行 empty" onclick="javascript:Foo();" /></div>
<script type="text/javascript" language="javascript">
<!--
function Foo()
{
document.selection.empty();
}
-->
</script>


selection的createRange方法

document.selection.createRange() 根據當前文字選擇返回 TextRange 對象,或根據控件選擇返回 ControlRange 對象。

配合 execCommand,在 HTML 編輯器中很有用,比如:文字加粗、斜體、復制、粘貼、創建超鏈接等。

這些好像都是只有在IE下才能實現。。

———————————————————————

images集合(頁面中的圖象)

a)通過集合引用
document.images //對應頁面上的img標簽
document.images.length //對應頁面上img標簽的個數
document.images[0] //第1個img標簽
document.images[i] //第i-1個img標簽

b)通過name屬性直接引用
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 /標簽與之對應就可以顯示

———————————————————————-

forms集合(頁面中的表單)

a)通過集合引用
document.forms //對應頁面上的form標簽
document.forms.length //對應頁面上/formform標簽的個數
document.forms[0] //第1個/formform標簽
document.forms[i] //第i-1個/formform標簽
document.forms[i].length //第i-1個/formform中的控件數
document.forms[i].elements[j] //第i-1個/formform中第j-1個控件

b)通過標簽name屬性直接引用
/formform name=”Myform”>input name=”myctrl”/>/form
document.Myform.myctrl //document.表單名.控件名

c)訪問表單的屬性
document.forms[i].name //對應form name>屬性
document.forms[i].action //對應/formform action>屬性
document.forms[i].encoding //對應/formform enctype>屬性
document.forms[i].target //對應/formform target>屬性

document.forms[i].appendChild(oTag) //動態插入一個控件
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

------------------------------------------------------------------------------------------------------------------------------------------------------------------
document.readyState:判斷文檔是否加載完成。firefox不支持。

這個屬性是只讀的,傳回值有以下的可能:

0-UNINITIALIZED:XML 對象被產生,但沒有任何文件被加載。
1-LOADING:加載程序進行中,但文件尚未開始解析。
2-LOADED:部分的文件已經加載且進行解析,但對象模型尚未生效。
3-INTERACTIVE:僅對已加載的部分文件有效,在此情況下,對象模型是有效但只讀的。
4-COMPLETED:文件已完全加載,代表加載成功。
復制代碼 代碼如下:
document.onreadystatechange = subSomething;//當頁面加載狀態改變的時候執行這個方法.
function subSomething()
{
if(document.readyState == "complete"){ //當頁面加載狀態為完全結束時進入
//你要做的操作。
}
else if(document.readyState=="loading"){
}
}

比較好的例子:http://www.jb51.net/article/20445.htm

說明 :onreadystatechange 事件能辨識readyState 屬性的改變。

document.all(只被IE支持)

action:document.layers是Netscape 4.x專有的屬性,是一個代表所有由儲如<div><layer>等定位了的元素的數組通常也是用<div> 或<layer>對象的id屬性來引用的,但是這裡面不包含除此以外的其它元素

document.layers和document.all的用法是一樣的,功能也是相同的。所在我就只介紹一種用法:
document.all的意思是文檔的所有元素,也就是說它包含了當前網頁的所有元素。它是以數組的形式保存元素的屬性的,所以我們可以用 document.all["元素名"].屬性名="屬性值"來動態改變元素的屬性。用這條語句,可以做出許許多多動態網頁效果,如:動態變換圖片、動態改變文本的背景、動態改變網頁的背景、動態改變圖片的大小、動態改變文字的大小各顏色等等。你簡直可以動態控制所有網頁元素。
document.all[]這個數組可以訪問文檔中所有元素。

例1(這個可以讓你理解文檔中哪些是對象)
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Document.All Example</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<h1>Example Heading</h1>
<hr />
<p>This is a <em>paragraph</em>. It is only a <em>paragraph.</em></p>
<p>Yet another <em>paragraph.</em></p>
<p>This final <em>paragraph</em> has <em id="special">special emphasis.</em></p>
<hr />
<script type="text/javascript">
<!--
var i,origLength;
origLength = document.all.length;
document.write('document.all.length='+origLength+"<br />");
for (i = 0; i < origLength; i++)
{
document.write("document.all["+i+"]="+document.all[i].tagName+"<br />");
}
//-->
</script>
</body>
</html>

它的執行結果是:
Example Heading

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

This is a paragraph. It is only a paragraph.
Yet another paragraph.
This final paragraph has special emphasis.

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

document.all.length=18
document.all[0]=!
document.all[1]=HTML
document.all[2]=HEAD
document.all[3]=TITLE
document.all[4]=META
document.all[5]=BODY
document.all[6]=H1
document.all[7]=HR
document.all[8]=P
document.all[9]=EM
document.all[10]=EM
document.all[11]=P
document.all[12]=EM
document.all[13]=P
document.all[14]=EM
document.all[15]=EM
document.all[16]=HR
document.all[17]=SCRIPT
(注意它只可以在IE上運行)
復制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>單擊DIV變色</title>
<style type="text/css">
<!--
#docid{
height:400px;
width:400px;
background-color:#999;}
-->
</style>
</head>
<body><div id="docid" name="docname" onClick="bgcolor()"></div>
</body>
</html>
<script language="javascript" type="text/javascript">
<!--
function bgcolor(){
document.all[7].style.backgroundColor="#000"
}
-->
</script>

上面的這個例子讓你了解怎麼訪問文檔中的一個特定元素,比如文檔中有一個DIV
<div id="docid" name="docname"></div>,你可以通過這個DIV的ID,NAME 或INDEX屬性訪問這個DIV:
document.all["docid"]
document.all["docname"]
document.all.item("docid")
document.all.item("docname")
document.all[7]
document.all.tags("div")則返回文檔中所有DIV數組,本例中只有一個DIV,所以用 document.all.tags("div")[0]就可以訪問了。

3、使用document.all[]
例3
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Document.All Example #2</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<!-- Works in Internet Explorer and compatible -->
<h1 id="heading1" align="center" style="font-size: larger;">DHTML Fun!!!</h1>
<form name="testform" id="testform" action="#" method="get">
<br /><br />
<input type="button" value="Align Left"
onclick="document.all['heading1'].align='left';" />//改變<h1& gt;</h1>標簽對中的align屬性的值,下面的代碼作用相同
<input type="button" value="Align Center"
onclick="document.all['heading1'].align='center';" />
<input type="button" value="Align Right"
onclick="document.all['heading1'].align='right';" />
<br /><br />
<input type="button" value="Bigger"
onclick="document.all['heading1'].style.fontSize='xx-large';" />
<input type="button" value="Smaller"
onclick="document.all['heading1'].style.fontSize='xx-small';" />
<br /><br />
<input type="button" value="Red"
onclick="document.all['heading1'].style.color='red';" />
<input type="button" value="Blue"
onclick="document.all['heading1'].style.color='blue';" />
<input type="button" value="Black"
onclick="document.all['heading1'].style.color='black';" />
<br /><br />
<input type="text" name="userText" id="userText" size="30" />
<input type="button" value="Change Text"
onclick="document.all['heading1'].innerText=document.testform.userText.value;" /& gt;//改變<h1></h1>標簽對中的文本內容
</form>
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved