2012-11-20 82 views
0

我想自动点击按钮我想自动点击按钮

但是怎么样?

我试过,但没有成功

<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(); 
+1

你ddnt包含对jQuery的引用 – SpYk3HH

回答

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/

3

实际上你需要包括jQuery的,你打电话之前$(function...

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 

您还需要调用.click()是在ready函数中。

3

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

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

你的代码有一些错误。

  • 您不包括jQuery的一个参考:<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  • “自动” 呼叫.click是文档。就绪功能之外
  • http://jsfiddle.net/SpYk3/nww4S/

您的代码,因为它SHOULD be:

<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>