最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS的Document屬性和方法小結

 更新時間:2013年09月17日 18:00:42   作者:  
Document想必大家并不陌生吧,在使用js的過程中會經(jīng)常遇到它,那么它有哪些屬性、哪些方法,在本文將以示例為大家詳細介紹下,希望對大家有所幫助
document.title //設置文檔標題等價于HTML的title標簽
document.bgColor //設置頁面背景色
document.fgColor //設置前景色(文本顏色)
document.linkColor //未點擊過的鏈接顏色
document.alinkColor //激活鏈接(焦點在此鏈接上)的顏色
document.vlinkColor //已點擊過的鏈接顏色
document.URL //設置URL屬性從而在同一窗口打開另一網(wǎng)頁
document.fileCreatedDate //文件建立日期,只讀屬性
document.fileModifiedDate //文件修改日期,只讀屬性
document.charset //設置字符集 簡體中文:gb2312
document.fileSize //文件大小,只讀屬性
document.cookie //設置和讀出cookie

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

document.write() //動態(tài)向頁面寫入內(nèi)容
document.createElement(Tag) //創(chuàng)建一個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) //動態(tài)生成一個HTML對象

常用對象事件

document.body.onclick=”func()” //鼠標指針單擊對象是觸發(fā)
document.body.onmouseover=”func()” //鼠標指針移到對象時觸發(fā)
document.body.onmouseout=”func()” //鼠標指針移出對象時觸發(fā)
———————————————————————
location-位置子對象

document.location.hash // #號后的部分
document.location.host // 域名+端口號//好像返回的是主機名localhost,沒有返回端口號
document.location.hostname // 域名
document.location.href // 完整URL
document.location.pathname // 目錄部分
document.location.port // 端口號
document.location.protocol // 網(wǎng)絡協(xié)議(http:)
document.location.search // ?號后的部分
documeny.location.reload() //刷新網(wǎng)頁
document.location.reload(URL) //打開新的網(wǎng)頁
document.location.assign(URL) //打開新的網(wǎng)頁
document.location.replace(URL) //打開新的網(wǎng)頁
———————————————————————
selection-選區(qū)子對象
document.selection

例如:
復制代碼 代碼如下:

<div>請選中這里的部分文字。</div>
<div><input type="button" value="請選中部分文字,然后點擊這里執(zhí)行 empty" onclick="javascript:Foo();" /></div>
<script type="text/javascript" language="javascript">
<!--
function Foo()
{
document.selection.empty();
}
-->
</script>


selection的createRange方法

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

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

這些好像都是只有在IE下才能實現(xiàn)。。

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

images集合(頁面中的圖象)

a)通過集合引用
document.images //對應頁面上的img標簽
document.images.length //對應頁面上img標簽的個數(shù)
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)創(chuàng)建一個圖象
var oImage
oImage = new Image()
document.images.oImage.src=”1.jpg”
同時在頁面上建立一個img /標簽與之對應就可以顯示

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

forms集合(頁面中的表單)

a)通過集合引用
document.forms //對應頁面上的form標簽
document.forms.length //對應頁面上/formform標簽的個數(shù)
document.forms[0] //第1個/formform標簽
document.forms[i] //第i-1個/formform標簽
document.forms[i].length //第i-1個/formform中的控件數(shù)
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) //動態(tài)插入一個控件
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 //動態(tài)輸出文本
document.getElementById(”ID”).innerHTML //動態(tài)輸出HTML
document.getElementById(”ID”).outerText //同innerText
document.getElementById(”ID”).outerHTML //同innerHTML

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

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

0-UNINITIALIZED:XML 對象被產(chǎn)生,但沒有任何文件被加載。
1-LOADING:加載程序進行中,但文件尚未開始解析。
2-LOADED:部分的文件已經(jīng)加載且進行解析,但對象模型尚未生效。
3-INTERACTIVE:僅對已加載的部分文件有效,在此情況下,對象模型是有效但只讀的。
4-COMPLETED:文件已完全加載,代表加載成功。
復制代碼 代碼如下:

document.onreadystatechange = subSomething;//當頁面加載狀態(tài)改變的時候執(zhí)行這個方法.
function subSomething()
{
if(document.readyState == "complete"){ //當頁面加載狀態(tài)為完全結束時進入
//你要做的操作。
}
else if(document.readyState=="loading"){
}
}

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

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

document.all(只被IE支持)

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

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

例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>

它的執(zhí)行結果是:
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數(shù)組,本例中只有一個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>標簽對中的文本內(nèi)容
</form>
</body>
</html>

相關文章

  • HTML5之lang屬性與dir屬性的詳解

    HTML5之lang屬性與dir屬性的詳解

    本篇文章是對HTML5中的lang屬性與dir屬性進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • javascript unicode與GBK2312(中文)編碼轉換方法

    javascript unicode與GBK2312(中文)編碼轉換方法

    本文提供了一個javascript的unicode與GBK2312編碼相互轉換的方法,大家可以參考使用,實用的小實例
    2013-11-11
  • 簡介JavaScript中的setTime()方法的使用

    簡介JavaScript中的setTime()方法的使用

    這篇文章主要介紹了簡介JavaScript中的setTime()方法的使用,是JS入門學習中的基礎知識,需要的朋友可以參考下
    2015-06-06
  • JavaScript的RequireJS庫入門指南

    JavaScript的RequireJS庫入門指南

    這篇文章主要介紹了JavaScript的RequireJS庫入門指南,RequireJS庫的人氣近來攀升很快,需要的朋友可以參考下
    2015-07-07
  • 總結javascript三元運算符知識點

    總結javascript三元運算符知識點

    這是一篇關于javascript三元運算符的相關基礎知識點內(nèi)容,大家可以學習一下鞏固基礎知識。
    2018-09-09
  • 不懂JavaScript應該怎樣學

    不懂JavaScript應該怎樣學

    文是JavaScript庫Mootools的入門性介紹文章,詳細介紹了在網(wǎng)上能找到的比較好的學習HTML,CSS,JavaScript入門知識的資源以及隨手參考的地方。文中介紹的從零入手學習JavaScript編程的過程很有參考價值。
    2008-04-04
  • JavaScript 事件綁定及深入

    JavaScript 事件綁定及深入

    這篇文章主要介紹了JavaScript 事件綁定及深入,需要的朋友可以參考下
    2015-04-04
  • javascript計時器事件使用詳解

    javascript計時器事件使用詳解

    通過使用JavaScript,能在一個設定的時間間隔之后來執(zhí)行代碼,而不是在函數(shù)被調用后立即執(zhí)行。我們稱之為計時事件,下面對javascript計時器事件做了詳細解釋
    2014-01-01
  • Javascript基礎教程之數(shù)據(jù)類型轉換

    Javascript基礎教程之數(shù)據(jù)類型轉換

    JavaScript是一種無類型語言,但同時JavaScript提供了一種靈活的自動類型轉換的處理方式。基本規(guī)則是,如果某個類型的值用于需要其他類型的值的環(huán)境中,JavaScript就自動將這個值轉換成所需要的類型。
    2015-01-01
  • 徹底搞懂?javascript的Promise

    徹底搞懂?javascript的Promise

    這篇文章主要為大家詳細介紹了javascript的Promise,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02

最新評論

定兴县| 钟山县| 虎林市| 同德县| 长泰县| 松原市| 金阳县| 临西县| 渝北区| 嘉荫县| 北海市| 康平县| 全州县| 泸定县| 灵武市| 龙井市| 宣汉县| 双柏县| 夹江县| 云浮市| 胶州市| 黄平县| 博爱县| 辉南县| 介休市| 盐源县| 信宜市| 蓬安县| 松溪县| 台南市| 吴旗县| 大同县| 县级市| 安徽省| 泗阳县| 阆中市| 庆安县| 上蔡县| 常州市| 正阳县| 连州市|