HTML DOM-->獲取屬性節點


1.方法一:

  獲取官方定義的屬性直接使用:

    元素節點.屬性名

  得到元素對應屬性的屬性值

  舉例:獲取placeholder的屬性值

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>js_excise</title>
        <script src="./js/js_excise.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body style="height: 187.5rem;">
        <input id="in" type="text" name="Text" placeholder="please your name">
        <script type="text/javascript">
            var js = document.getElementById('in')
                console.log(js.placeholder)
        </script>
    </body>
</html>

  輸出:

 

  修改對應屬性的屬性值:

    元素節點.屬性名=新的屬性值

  舉例:將placeholder="please your name"改為‘please enter your name’

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>js_excise</title>
        <script src="./js/js_excise.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body style="height: 187.5rem;">
        <button onclick="func()">更新</button>
        <input id="in" type="text" name="Text" placeholder="please your name">
        <script type="text/javascript">
            var js = document.getElementById('in')
                console.log(js.placeholder)
            function func(){
                js.placeholder = 'please enter your name'
                console.log(js.placeholder)
            }
        </script>
    </body>
</html>

 

  輸出:

 

 2.方法二:

  元素節點.getAttribute("屬性名")

  得到元素對應屬性的屬性值

  注意:該方法還可以獲取自定義屬性

  舉例1:獲取placeholder屬性的值

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>js_excise</title>
        <script src="./js/js_excise.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body style="height: 187.5rem;">
        <button onclick="func()">更新</button>
        <input id="in" type="text" name="Text" placeholder="please your name">
        <script type="text/javascript">
            var js = document.getElementById('in')
            console.log(js.getAttribute('placeholder'))
        </script>
    </body>
</html>

  輸出:

 

   舉例2:獲取自定義屬性my的值

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>js_excise</title>
        <script src="./js/js_excise.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body style="height: 187.5rem;">
        <button onclick="func()">更新</button>
        <input id="in" type="text" name="Text" placeholder="please your name" my='abner'>
        <script type="text/javascript">
            var js = document.getElementById('in')
            console.log(js.getAttribute('my'))
        </script>
    </body>
</html>

  輸出:

  修改元素對應屬性的屬性值

    元素節點.setAttribute("屬性名",“新的屬性值”)

  舉例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>js_excise</title>
        <script src="./js/js_excise.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body style="height: 187.5rem;">
        <button onclick="func()">更新</button>
        <input id="in" type="text" name="Text" placeholder="please your name" my='abner'>
        <script type="text/javascript">
            var js = document.getElementById('in')
            console.log('my更改前:'+js.getAttribute('my'))
        function func(){
            js.setAttribute('my','hello world')
            console.log('my更改后:'+js.getAttribute('my'))
        }
        </script>
    </body>
</html>

  輸出:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM