jQuery:触发click()不起作用? [英] jQuery: trigger click() doesn't work?

查看:109
本文介绍了jQuery:触发click()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么单击trigger1trigger2不能触发,请单击open吗?

Why clicking on trigger1 and trigger2 doesn't fire click on open ?

<a id="trigger1" href="#" onclick="jQuery('#open').trigger('click');">trigger1</a>  
<a id="trigger2" href="#" onclick="jQuery('#open').click();">trigger2</a>
<a id="open" href="http://google.com">open</a>

使用就绪(trigger3)也不起作用:

<a id="trigger3" href="#">trigger3</a>

...

jQuery(document).ready(function(){    
  jQuery('#trigger3').bind('click', function(){
      jQuery('#open').html('to be fired'); /* works */
      jQuery('#open').click();        
  });

  jQuery('#trigger3').click(function(){
      jQuery('#open').html('to be fired'); /* works */
      jQuery('#open').click();
  });
});

推荐答案

重要的是要澄清,执行jQuery('#open').click()不会执行锚标记的href属性,因此您不会被重定向.它为未定义的#open执行onclick事件.

It's important to clarify that doing jQuery('#open').click() does not execute the href attribute of an anchor tag so you will not be redirected. It executes the onclick event for #open which is not defined.

您可以通过给#open一个click事件来完成重定向以及使用原始jQuery('#open').click()代码引起重定向的功能:

You can accomplish the redirect and the ability to cause it with your original jQuery('#open').click() code by giving #open a click event:

jQuery('#open').click( function (e) {
  window.location.href = this.href;
});

这篇关于jQuery:触发click()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆