JavaScript中常見的幾種獲取元素的方式
1.根據(jù)id獲取元素
document.getElementById("id屬性的值");
返回值是一個元素對象
案例:點擊按鈕彈框
<body><input type="button" value="彈框" id="btn"><script> //根據(jù)id屬性的值從文檔中獲取這個元素 var btnobj = document.getElementById("btn"); //為當前的這個按鈕元素(對象),注冊點擊事件,添加事件處理函數(shù)(匿名函數(shù)) btnobj.onclick = function () {//響應做的事情alert("碼仙"); };</script></body>
2.根據(jù)標簽名字獲取元素 document.getElementsByTagName("標簽的名字");
返回值是一個偽數(shù)組
案例:點擊按鈕改變多個p標簽的文字內(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獲取按鈕,注冊點擊事件,添加事件處理函數(shù) document.getElementById("btn").onclick = function () {//根據(jù)標簽名字獲取標簽var pObjs = document.getElementsByTagName("p");//var pObjs=document.getElementById("dv1").getElementsByTagName("p");//循環(huán)遍歷這個數(shù)組for (var i = 0; i < pObjs.length; i++) { //每個p標簽,設置文字 pObjs[i].innerText = "我們都是p";} };</script></body>
3.根據(jù)name屬性的值獲取元素 document.getElementsByName("name屬性的值");
返回值是一個偽數(shù)組
案例:案例:點擊按鈕,改變所有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> //點擊按鈕,改變所有name屬性值為name1的文本框中的value屬性值 document.getElementById("btn").onclick = function () {//通過name屬性值獲取元素-------表單的標簽var inputs = document.getElementsByName("name1");for (var i = 0; i < inputs.length; i++) { inputs[i].value = "我很好";} };</script></body>
4.根據(jù)類樣式的名字獲取元素 document.getElementsByClassName("類樣式的名字");
返回值是一個偽數(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獲取按鈕,為按鈕注冊點擊事件,添加事件處理函數(shù) document.getElementById("btn").onclick = function () {//獲取所有的文本框//根據(jù)類樣式的名字獲取元素var inputs = document.getElementsByClassName("text");for (var i = 0; i < inputs.length; i++) { inputs[i].value = "碼仙";} };</script></body>
5.根據(jù)選擇器獲取元素 1.document.querySelector("選擇器");
返回值是一個元素對象
案例:點擊按鈕彈框
<body><input type="button" value="顯示效果1" id="btn"/><input type="button" value="顯示效果2"/><script> //點擊按鈕彈出對話框 //根據(jù)選擇器的方式獲取元素 var btnObj1 = document.querySelector("#btn"); btnObj1.onclick = function () {alert("我變帥了"); }; var btnObj2 = document.querySelector(".btn"); btnObj2.onclick = function () {alert("哈哈,我又變帥了"); };</script></body>
2.document.querySelectorAll("選擇器");
返回值是一個偽數(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中常見的幾種獲取元素的方式的文章就介紹到這了,更多相關(guān)js 獲取元素內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. vue實現(xiàn)web在線聊天功能2. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis3. JavaScript實現(xiàn)頁面動態(tài)驗證碼的實現(xiàn)示例4. Springboot 全局日期格式化處理的實現(xiàn)5. Java使用Tesseract-Ocr識別數(shù)字6. 完美解決vue 中多個echarts圖表自適應的問題7. Python使用urlretrieve實現(xiàn)直接遠程下載圖片的示例代碼8. SpringBoot+TestNG單元測試的實現(xiàn)9. 在Chrome DevTools中調(diào)試JavaScript的實現(xiàn)10. 解決Android Studio 格式化 Format代碼快捷鍵問題
