JavaScript中常見(jiàn)的幾種獲取元素的方式
1.根據(jù)id獲取元素
document.getElementById("id屬性的值");
返回值是一個(gè)元素對(duì)象
案例:點(diǎn)擊按鈕彈框
<body><input type="button" value="彈框" id="btn"><script> //根據(jù)id屬性的值從文檔中獲取這個(gè)元素 var btnobj = document.getElementById("btn"); //為當(dāng)前的這個(gè)按鈕元素(對(duì)象),注冊(cè)點(diǎn)擊事件,添加事件處理函數(shù)(匿名函數(shù)) btnobj.onclick = function () {//響應(yīng)做的事情alert("碼仙"); };</script></body>
2.根據(jù)標(biāo)簽名字獲取元素 document.getElementsByTagName("標(biāo)簽的名字");
返回值是一個(gè)偽數(shù)組
案例:點(diǎn)擊按鈕改變多個(gè)p標(biāo)簽的文字內(nèi)容
<body><input type="button" value="改變" id="btn"><div id="dv"> <p>哈哈,我又變帥了</p> <p>哈哈,我又變帥了</p> <p>哈哈,我又變帥了</p> <p>哈哈,我又變帥了</p> <p>哈哈,我又變帥了</p></div><script> //根據(jù)id獲取按鈕,注冊(cè)點(diǎn)擊事件,添加事件處理函數(shù) document.getElementById("btn").onclick = function () {//根據(jù)標(biāo)簽名字獲取標(biāo)簽var pObjs = document.getElementsByTagName("p");//var pObjs=document.getElementById("dv1").getElementsByTagName("p");//循環(huán)遍歷這個(gè)數(shù)組for (var i = 0; i < pObjs.length; i++) { //每個(gè)p標(biāo)簽,設(shè)置文字 pObjs[i].innerText = "我們都是p";} };</script></body>
3.根據(jù)name屬性的值獲取元素 document.getElementsByName("name屬性的值");
返回值是一個(gè)偽數(shù)組
案例:案例:點(diǎn)擊按鈕,改變所有name屬性值為name1的文本框中的value屬性值
<body><input type="button" value="顯示效果" id="btn"/><br/><input type="text" value="您好" name="name1"/><br/><input type="text" value="您好" name="name2"/><br/><input type="text" value="您好" name="name1"/><br/><input type="text" value="您好" name="name3"/><br/><input type="text" value="您好" name="name1"/><br/><input type="text" value="您好" name="name1"/><br/><script> //點(diǎn)擊按鈕,改變所有name屬性值為name1的文本框中的value屬性值 document.getElementById("btn").onclick = function () {//通過(guò)name屬性值獲取元素-------表單的標(biāo)簽var inputs = document.getElementsByName("name1");for (var i = 0; i < inputs.length; i++) { inputs[i].value = "我很好";} };</script></body>
4.根據(jù)類(lèi)樣式的名字獲取元素 document.getElementsByClassName("類(lèi)樣式的名字");
返回值是一個(gè)偽數(shù)組
案例:修改所有文本框的值
<body><input type="button" value="修改文本框的值" id="btn"/><br/><input type="text" value=""/><br/><input type="text" value=""/><br/><input type="text" value=""/><script> //根據(jù)id獲取按鈕,為按鈕注冊(cè)點(diǎn)擊事件,添加事件處理函數(shù) document.getElementById("btn").onclick = function () {//獲取所有的文本框//根據(jù)類(lèi)樣式的名字獲取元素var inputs = document.getElementsByClassName("text");for (var i = 0; i < inputs.length; i++) { inputs[i].value = "碼仙";} };</script></body>
5.根據(jù)選擇器獲取元素 1.document.querySelector("選擇器");
返回值是一個(gè)元素對(duì)象
案例:點(diǎn)擊按鈕彈框
<body><input type="button" value="顯示效果1" id="btn"/><input type="button" value="顯示效果2"/><script> //點(diǎn)擊按鈕彈出對(duì)話(huà)框 //根據(jù)選擇器的方式獲取元素 var btnObj1 = document.querySelector("#btn"); btnObj1.onclick = function () {alert("我變帥了"); }; var btnObj2 = document.querySelector(".btn"); btnObj2.onclick = function () {alert("哈哈,我又變帥了"); };</script></body>
2.document.querySelectorAll("選擇器");
返回值是一個(gè)偽數(shù)組
案例:修改所有文本框的值
<body><input type="button" value="修改文本框的值" id="btn"/><br/><input type="text" value=""/><br/><input type="text" value=""/><br/><input type="text" value=""/><script> document.getElementById("btn").onclick = function () {//根據(jù)選擇器的方式獲取元素var inputs = document.querySelectorAll(".text");for (var i = 0; i < inputs.length; i++) { inputs[i].value = "碼仙";} };</script></body>
到此這篇關(guān)于JavaScript中常見(jiàn)的幾種獲取元素的方式的文章就介紹到這了,更多相關(guān)js 獲取元素內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. XML入門(mén)的常見(jiàn)問(wèn)題(四)2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. 詳解CSS偽元素的妙用單標(biāo)簽之美4. 詳解PHP實(shí)現(xiàn)HTTP服務(wù)器過(guò)程5. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. XML入門(mén)的常見(jiàn)問(wèn)題(一)9. HTML DOM setInterval和clearInterval方法案例詳解10. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法
