V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
cuthead

为什么只有在 function 里才能 alert document.getElementById("text1").value,无法直接 alert document.getElementById("text1").value

  •  
  •   cuthead · Apr 3, 2015 · 2908 views
    This topic created in 4043 days ago, the information mentioned may be changed or developed.
    <html>
    <head>
    <script type="text/javascript">
    function alertValue()
    {
    alert(document.getElementById("text1").value)
    }
    </script>
    </head>
    <body>

    <form>
    <input type="text" id="text1" value="Hello world!" />
    <input type="button" id="button1" onclick="alertValue()"
    value="Show default value" />
    </form>

    </body>
    </html>

    我想这样
    <html>
    <head>
    <script type="text/javascript">
    alert(document.getElementById("text1").value)
    </script>
    </head>
    <body>

    <form>
    <input type="text" id="text1" value="Hello world!" />
    <input type="button" id="button1" onclick="alertValue()"
    value="Show default value" />
    </form>

    </body>
    </html>
    就无法弹出alert,何解?
    tux
        1
    tux  
       Apr 3, 2015
    因为alert的时候,要alert出来的东西还没有加载,把alert放在要alert的东西底下就好了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3721 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:42 · PVG 12:42 · LAX 21:42 · JFK 00:42
    ♥ Do have faith in what you're doing.