js 頁面元素的幾個(gè)用法總結(jié)
對(duì)象屬性
document.title //設(shè)置文檔標(biāo)題等價(jià)于HTML的title標(biāo)簽
document.bgColor //設(shè)置頁面背景色
document.fgColor //設(shè)置前景色(文本顏色)
document.linkColor //未點(diǎn)擊過的鏈接顏色
document.alinkColor //激活鏈接(焦點(diǎn)在此鏈接上)的顏色
document.vlinkColor //已點(diǎn)擊過的鏈接顏色
document.URL //設(shè)置URL屬性從而在同一窗口打開另一網(wǎng)頁
document.fileCreatedDate //文件建立日期,只讀屬性
document.fileModifiedDate //文件修改日期,只讀屬性
document.charset //設(shè)置字符集 簡(jiǎn)體中文:gb2312
document.fileSize //文件大小,只讀屬性
document.cookie //設(shè)置和讀出cookie
———————————————————————
常用對(duì)象方法
document.write() //動(dòng)態(tài)向頁面寫入內(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)擊過的鏈接顏色
document.body.alink //激活鏈接(焦點(diǎn)在此鏈接上)的顏色
document.body.vlink //已點(diǎn)擊過的鏈接顏色
document.body.text //文本色
document.body.innerText //設(shè)置body>…/body>之間的文本
document.body.innerHTML //設(shè)置body>…/body>之間的HTML代碼
document.body.topMargin //頁面上邊距
document.body.leftMargin //頁面左邊距
document.body.rightMargin //頁面右邊距
document.body.bottomMargin //頁面下邊距
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)//好像返回的是主機(jī)名localhost,沒有返回端口號(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)頁
document.location.reload(URL) //打開新的網(wǎng)頁
document.location.assign(URL) //打開新的網(wǎng)頁
document.location.replace(URL) //打開新的網(wǎng)頁
———————————————————————
selection-選區(qū)子對(duì)象
document.selection
例如:
<div>請(qǐng)選中這里的部分文字。</div>
<div><input type="button" value="請(qǐng)選中部分文字,然后點(diǎn)擊這里執(zhí)行 empty" onclick="javascript:Foo();" /></div>
<script type="text/javascript" language="javascript">
function Foo()
{
document.selection.empty();
}
</script>
selection的createRange方法
document.selection.createRange() 根據(jù)當(dāng)前文字選擇返回 TextRange 對(duì)象,或根據(jù)控件選擇返回 ControlRange 對(duì)象。
配合 execCommand,在 HTML 編輯器中很有用,比如:文字加粗、斜體、復(fù)制、粘貼、創(chuàng)建超鏈接等。
這些好像都是只有在IE下才能實(shí)現(xiàn)。。
———————————————————————
images集合(頁面中的圖象)
a)通過集合引用
document.images //對(duì)應(yīng)頁面上的img標(biāo)簽
document.images.length //對(duì)應(yīng)頁面上img標(biāo)簽的個(gè)數(shù)
document.images[0] //第1個(gè)img標(biāo)簽
document.images[i] //第i-1個(gè)img標(biāo)簽
b)通過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í)在頁面上建立一個(gè)img /標(biāo)簽與之對(duì)應(yīng)就可以顯示
———————————————————————-
forms集合(頁面中的表單)
a)通過集合引用
document.forms //對(duì)應(yīng)頁面上的form標(biāo)簽
document.forms.length //對(duì)應(yīng)頁面上/formform標(biāo)簽的個(gè)數(shù)
document.forms[0] //第1個(gè)/formform標(biāo)簽
document.forms[i] //第i-1個(gè)/formform標(biāo)簽
document.forms[i].length //第i-1個(gè)/formform中的控件數(shù)
document.forms[i].elements[j] //第i-1個(gè)/formform中第j-1個(gè)控件
b)通過標(biāo)簽name屬性直接引用
/formform name=”Myform”>input name=”myctrl”/>/form
document.Myform.myctrl //document.表單名.控件名
c)訪問表單的屬性
document.forms[i].name //對(duì)應(yīng)form name>屬性
document.forms[i].action //對(duì)應(yīng)/formform action>屬性
document.forms[i].encoding //對(duì)應(yīng)/formform enctype>屬性
document.forms[i].target //對(duì)應(yīng)/formform target>屬性
document.forms[i].appendChild(oTag) //動(dòng)態(tài)插入一個(gè)控件
document.all.oDiv //引用圖層oDiv
document.all.oDiv.style.display=”" //圖層設(shè)置為可視
document.all.oDiv.style.display=”none” //圖層設(shè)置為隱藏
document.getElementId(”oDiv”) //通過getElementId引用對(duì)象
document.getElementId(”oDiv”).style=”"
document.getElementId(”oDiv”).display=”none”
/*document.all表示document中所有對(duì)象的集合
只有ie支持此屬性,因此也用來判斷瀏覽器的種類*/
圖層對(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
相關(guān)文章
VsCode插件自動(dòng)生成注釋插件koroFileHeader使用教程
這篇文章主要介紹了VsCode插件自動(dòng)生成注釋插件koroFileHeader使用教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
PHPMyAdmin導(dǎo)入時(shí)提示文件大小超出PHP限制的解決方法
這篇文章主要介紹了PHPMyAdmin導(dǎo)入時(shí)提示文件大小超出PHP限制的解決方法,造成這個(gè)問題的原因是PHP上傳大小限制為2MB,修改PHP.ini配置即可解決這問題,需要的朋友可以參考下2015-03-03
JS日期轉(zhuǎn)化指定格式及獲取月/周日期區(qū)間的方法
本文詳細(xì)介紹了JavaScript中Date類型的處理方法,包括日期的格式轉(zhuǎn)換和獲取特定月份或周的日期區(qū)間,本文介紹JS日期轉(zhuǎn)化指定格式及獲取月/周日期區(qū)間的方法,感興趣的朋友跟隨小編一起看看吧2024-09-09
JavaScript 抽獎(jiǎng)效果實(shí)現(xiàn)代碼 數(shù)字跳動(dòng)版
直接上代碼吧,效果可以復(fù)制了去看2009-11-11
在layui中使用form表單監(jiān)聽ajax異步驗(yàn)證注冊(cè)的實(shí)例
今天小編就為大家分享一篇在layui中使用form表單監(jiān)聽ajax異步驗(yàn)證注冊(cè)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JS下拉框內(nèi)容左右移動(dòng)效果的具體實(shí)現(xiàn)
這篇文章介紹了JS下拉框內(nèi)容左右移動(dòng)效果的具體實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-07-07

