代码之家  ›  专栏  ›  技术社区  ›  Babiker

未触发Javascript事件

  •  0
  • Babiker  · 技术社区  · 14 年前

    #theButton 则应触发警报。为什么没有使用以下方法触发任何事件?

                var theButton = document.getElementById("theButton");
           
                theButton.onclick = function(){alert("Clicked!");}
                theButton.onclick = clickAlert;
                theButton.onclick(alert("Clicked!"));
        
                function clickAlert(){
                    alert("Clicked!");
                }
            </script>
        </head>
        <body>
            <input type="button" id="theButton">
        </body>
    </html>
    
    5 回复  |  直到 4 年前
        1
  •  2
  •   Peter Bailey    14 年前

    你有三个问题

    1. 你拼错了 function
    2. 您尝试在节点在DOM中可用之前访问该节点#按钮。听佩卡的建议
    3. onclick 物业在8号线-不知道你是故意的还是怎么做的
        2
  •  1
  •   Pekka    14 年前

    theButton 运行脚本时还不存在?

    把它包进身体的 onload ready() .

        3
  •  1
  •   Brandon    14 年前

    尝试将其包装到document.Ready()函数中。

        4
  •  1
  •   Seattle Leonard    14 年前

    我喜欢这个 jQuery.ready() 回答,但我相信你也可以移动 <script> 当你按下页面末尾的按钮时,把它移到某个地方。

        5
  •  1
  •   T.T.T.    14 年前

    <html>
         <head>
          <script type="text/javascript" language="javascript">
    
    
    
           function clickAlert(){
            alert("Clicked!");       
    
           }
    
          </script>
         </head>
         <body>
          <input type="button" id="theButton" NAME="theButton" onclick="clickAlert()">
         </body>
        </html>
    
    推荐文章