溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JavaScript中有哪些獲取元素的方法

發(fā)布時(shí)間:2021-07-19 00:41:30 來(lái)源:億速云 閱讀:123 作者:chen 欄目:web開(kāi)發(fā)

這篇文章主要介紹“JavaScript中有哪些獲取元素的方法”,在日常操作中,相信很多人在JavaScript中有哪些獲取元素的方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”JavaScript中有哪些獲取元素的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

JavaScript中獲取元素的方法有:1、根據(jù)id獲取元素;2、根據(jù)標(biāo)簽名字獲取元素;3、根據(jù)name屬性的值獲取元素;4、根據(jù)類樣式的名字獲取元素;5、根據(jù)選擇器獲取元素。

本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。

1.根據(jù)id獲取元素

document.getElementById("id屬性的值");

2.根據(jù)標(biāo)簽名字獲取元素

document.getElementsByTagName("標(biāo)簽的名字");

3.根據(jù)name屬性的值獲取元素

document.getElementsByName("name屬性的值");

4.根據(jù)類樣式的名字獲取元素

document.getElementsByClassName("類樣式的名字");

5.根據(jù)選擇器獲取元素

1.document.querySelector("選擇器");

2.document.querySelectorAll("選擇器");

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">
    <p id="dv">
        <p>哈哈,我又變帥了</p>
        <p>哈哈,我又變帥了</p>
        <p>哈哈,我又變帥了</p>
        <p>哈哈,我又變帥了</p>
        <p>哈哈,我又變帥了</p>
    </p>
    <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ù)類樣式的名字獲取元素

document.getElementsByClassName("類樣式的名字");

返回值是一個(gè)偽數(shù)組

案例:修改所有文本框的值

<body>
    <input type="button" value="修改文本框的值" id="btn"/><br/>
    <input type="text" value="" class="text"/><br/>
    <input type="text" value="" class="text"/><br/>
    <input type="text" value="" class="text"/>
    <script>
        //根據(jù)id獲取按鈕,為按鈕注冊(cè)點(diǎn)擊事件,添加事件處理函數(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("選擇器");

返回值是一個(gè)元素對(duì)象

案例:點(diǎn)擊按鈕彈框

<body>
    <input type="button" value="顯示效果1" id="btn"/>
    <input type="button" value="顯示效果2" class="btn"/>
    <script>
        //點(diǎn)擊按鈕彈出對(duì)話框
        //根據(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="" class="text"/><br/>
    <input type="text" value="" class="text"/><br/>
    <input type="text" value="" class="text"/>
    <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中有哪些獲取元素的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI