js-script標簽放在的位置


* 建議把script標簽放到</body>后面
  * 如果現在有這樣一個需求
  在js里面需要獲取到input里面的值,如果把script標簽放到head里面,會出現問題。HTML解析是從上到下解析的,script標簽放到head里面,直接在里面取input里面的值,因為頁面
還沒有解析到input那一行,肯定取不到!

<html>
    <head>
        <title>World</title>
        <style type="text/css">
        </style>
        <!-- 放在此處解析不到
        <script type="text/javascript">
        var input1 = document.getElementById("nameid");
        alert(input1.value);
        -->
    </script>
    </head>
    <body>
        <input type="text" name="name" id="nameid" value="aaaaa">
    </body>
    <script type="text/javascript">
        var input1 = document.getElementById("nameid");
        alert(input1.value);
    </script>
</html>

 


免責聲明!

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



猜您在找 應該把script標簽放在哪里 JS應該放在什么位置? (轉)script標簽到底該放在哪里 HTML中的