查看完整版本: 關於 JS 此匿名函式註冊事件的問題!!
頁: [1]

mb7773016 發表於 2016-5-14 05:15 PM

關於 JS 此匿名函式註冊事件的問題!!

本帖最後由 mb7773016 於 2016-5-14 05:16 PM 編輯

請問此code 想在前端click button 觸發抓取 Textbox value ,但似乎click button 無任何反應,求助是哪邊code有問題??{:36:}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script>
        function body() {
            document.getElementById("onload_Test").addEventListener("load", function () {
                 document.getElementById("Button3").addEventListener("click",function () {
                    alert("Hello 3 , " + document.getElementById("Text1").value);
                });
            });
        }
   </script>
</head>
<body id="onload_Test">
    Name : <input id="Text1" type="text" value="John" />
    <input id="Button3" type="button" value="This is first pae" />
</body>
</html>



...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

chengpocheng 發表於 2016-5-16 04:47 PM

本帖最後由 chengpocheng 於 2016-5-16 04:47 PM 編輯

誠如sheauren大大說的,您並沒有註冊你的body function,所以修改的地方有兩個

1. body不用設ID
2. 註冊click監聽事件 (body onload)<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        function body() {

            document.getElementById("Button3").addEventListener("click",function () {
                    alert("Hello 3 , " + document.getElementById("Text1").value);
                });
        }
   </script>
</head>
<body onload="body()">
    Name : <input id="Text1" type="text" value="John" />
    <input id="Button3" type="button" value="This is first pae" />
</body>
</html>下面再提供另一種button onclick方式<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        function getText1Value()
        {
            alert("Hello 3 ," + document.getElementById("Text1").value);
        }
   </script>
</head>
<body>
    Name : <input id="Text1" type="text" value="John" />
    <input id="Button3" type="button" value="This is first pae" onclick="getText1Value()"/>
</body>
</html>...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

mb7773016 發表於 2016-5-16 07:38 PM

本帖最後由 mb7773016 於 2016-5-16 07:38 PM 編輯

chengpocheng 發表於 2016-5-16 04:47 PM static/image/common/back.gif
誠如sheauren大大說的,您並沒有註冊你的body function,所以修改的地方有兩個

1. body不用設ID

Thanks for your help !! ( sheauren & chengpocheng )
頁: [1]