Javascript Jquery click函数不触发单击

Javascript Jquery click函数不触发单击,javascript,jquery,Javascript,Jquery,我有一个简单的HTML页面: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script> <script> $(document).ready(function () { $('#test').click(); }); </script> </head>

我有一个简单的HTML页面:

<html>

<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript">  </script>

<script>
$(document).ready(function () {
  $('#test').click();
});
</script>

</head>

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

</html>

$(文档).ready(函数(){
$(“#测试”)。单击();
});
我想在加载页面时触发名为
test
的元素上的单击事件。实际的代码实际上比这更复杂,但这个非常基本的示例不起作用。我检查了控制台日志,那里没有任何通知。

您正在触发的
.click()
是正确的,但是您缺少
“#test”
的单击事件,如图所示:

$('#test').click(function(){
  alert('here');
});
<script>
$(document).ready(function () {
  $('#test').click();  //or  $('#test').trigger('click');
  $('#test').click(function(e){
    e.preventDefault();
    alert('here');
    window.location.href = $(this).attr('href'); //redirect to specified href
  });
});
</script>
现在,完整的jquery代码如下所示:

$('#test').click(function(){
  alert('here');
});
<script>
$(document).ready(function () {
  $('#test').click();  //or  $('#test').trigger('click');
  $('#test').click(function(e){
    e.preventDefault();
    alert('here');
    window.location.href = $(this).attr('href'); //redirect to specified href
  });
});
</script>

$(文档).ready(函数(){
$('#test')。单击();//或$('#test')。触发器('click');
$(“#测试”)。单击(函数(e){
e、 预防默认值();
警报(“此处”);
window.location.href=$(this.attr('href');//重定向到指定的href
});
});
您正在触发的
.click()
是正确的,但是您缺少
'#test'
的单击事件,如图所示:

$('#test').click(function(){
  alert('here');
});
<script>
$(document).ready(function () {
  $('#test').click();  //or  $('#test').trigger('click');
  $('#test').click(function(e){
    e.preventDefault();
    alert('here');
    window.location.href = $(this).attr('href'); //redirect to specified href
  });
});
</script>
现在,完整的jquery代码如下所示:

$('#test').click(function(){
  alert('here');
});
<script>
$(document).ready(function () {
  $('#test').click();  //or  $('#test').trigger('click');
  $('#test').click(function(e){
    e.preventDefault();
    alert('here');
    window.location.href = $(this).attr('href'); //redirect to specified href
  });
});
</script>

$(文档).ready(函数(){
$('#test')。单击();//或$('#test')。触发器('click');
$(“#测试”)。单击(函数(e){
e、 预防默认值();
警报(“此处”);
window.location.href=$(this.attr('href');//重定向到指定的href
});
});
.click()实际上并不发送click事件

.click()的作用是定义单击元素时发生的事件。

。click()实际上并不发送单击事件

.click()所做的是定义当您单击元素时发生的事情。

您的#测试链接应该绑定到一个函数,例如:

$(document).ready(function () {

  //do something on test click
  $('#test').on("click", alert());

 //click test
  $('#test').click();
});

您的#测试链接应该绑定到一个函数,例如:

$(document).ready(function () {

  //do something on test click
  $('#test').on("click", alert());

 //click test
  $('#test').click();
});

您需要使用来触发DOM元素单击

$('#test').get(0).click();
使用时,它检索jQuery对象匹配的DOM元素。作为
。click()
将触发元素click事件处理程序。它实际上不会单击该元素

使用简单的Vanialla JS

document.getElementById('test').click()

您需要使用来触发DOM元素单击

$('#test').get(0).click();
使用时,它检索jQuery对象匹配的DOM元素。作为
。click()
将触发元素click事件处理程序。它实际上不会单击该元素

使用简单的Vanialla JS

document.getElementById('test').click()

此处会触发click事件,即在页面加载时触发click事件,但您不能仅通过触发click事件加载href,因为您可以使用


window.location.href=“”

此处会触发单击事件,即在页面加载时触发单击事件,但您不能仅通过触发单击事件加载href,因为您可以使用


window.location.href=“”

您必须使用$('#test')。触发器(“单击”)@Regent否如果OP想要触发单击事件,则必须执行的操作将驻留在him@ChakravarthySM当您调用
时,请查看发生了什么。单击()
不带参数。@Satpal部分正确,部分正确:
$('#测试')。获取()。单击()将导致错误<代码>$(“#测试”)。获取(0)。单击()不会导致错误。@Regent,是的,您是正确的。我在评论中遗漏了
0
。您必须使用$('test')。触发器(“单击”)@Regent否如果OP想要触发单击事件,则必须执行的操作将驻留在him@ChakravarthySM当您调用
时,请查看发生了什么。单击()
不带参数。@Satpal部分正确,部分正确:
$('#测试')。获取()。单击()将导致错误<代码>$(“#测试”)。获取(0)。单击()不会导致错误。@Regent,是的,您是正确的。我在评论中遗漏了
0