我想自动点击按钮

时间:2012-11-20 22:16:54

标签: jquery

我想自动点击按钮

但是怎么样?

我尝试了这个但没有成功

 <html>
 <head>

 <script>
 $(function(){
 $("a").click(function(){
alert("hillo");
});
 });

$("a").click();

  </script>
</head>

<body>

<a href="http://google.com">google</a>


</body>

</html>

此井仅用于但未成功

$("a").click();

4 个答案:

答案 0 :(得分:3)

在调用$(function...

之前,您需要实际包含jQuery
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

您还需要调用.click()才能进入就绪功能。

答案 1 :(得分:3)

在文档就绪事件之前,将调用$("a").click()的方式。如果将它放在同一个文档就绪处理程序中,它应该可以工作。

$(function(){
    $("a").click(function(){
        alert("hillo");
    });
    $("a").click();
});

答案 2 :(得分:2)

您的代码存在一些问题。

  • 您没有包含对jQuery的引用:<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  • .click的“自动”调用超出了文档.ready function
  • http://jsfiddle.net/SpYk3/nww4S/
  

您的代码应该

<html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("a").click(function(e) { alert("hillo"); });

            $("a").click();
        });
    </script>
    </head>
    <body>
        <a href="http://google.com">google</a>
    </body>
</html>

答案 3 :(得分:0)

尝试在DOM handler ..

中触发事件

click() evnet在被分配之前被解雇,甚至,正如您所写outside the DOM handler

假设您已包含jQuery文件

$(function(){
    $("a").click(function(){  // $("a").on('click', function()   
                              //  this is better
       alert("hillo");
     });

    $("a").click();   // Moved it inside
 });

http://jsfiddle.net/sushanth009/XwjYj/

相关问题