首頁 > web前端 > js教程 > 主體

js 頁面元素的幾個用法總結_javascript技巧

WBOY
發布: 2016-05-16 17:14:41
原創
839 人瀏覽過

物件屬性

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>請選取這裡的部分文字。

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!