如何使用 jQuery 触发对链接的点击 [英] How to trigger a click on a link using jQuery

查看:29
本文介绍了如何使用 jQuery 触发对链接的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接:

<ul id="titleee" class="gallery">
  <li>
    <a href="#inline" rel="prettyPhoto">Talent</a>
  </li>
</ul>

我正在尝试使用以下方法触发它:

and I am trying to trigger it by using:

$(document).ready(function() {
  $('#titleee').find('a').trigger('click');
});

但它不起作用.

我也试过:$('#titleee a').trigger('click');

编辑:

我实际上需要触发这里调用的任何 get <a href="#inline" rel="prettyPhoto">

I actually need to trigger whatever get's called here <a href="#inline" rel="prettyPhoto">

推荐答案

如果您尝试在锚点上触发事件,那么您拥有的代码将起作用 我在 jsfiddle 并添加了 eventHandler 以便您可以看到它的工作原理:

If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added eventHandler so you can see that it works:

$(document).on("click", "a", function(){
    $(this).text("It works!");
});

$(document).ready(function(){
    $("a").trigger("click");
});

您是要让用户通过单击锚点导航到网页上的某个点,还是要触发绑定到它的事件?可能你还没有真正将点击事件成功绑定到事件上?

Are you trying to cause the user to navigate to a certain point on the webpage by clicking the anchor, or are you trying to trigger events bound to it? Maybe you haven't actually bound the click event successfully to the event?

还有这个:

$('#titleee').find('a').trigger('click');

相当于:

$('#titleee a').trigger('click');

无需调用查找.:)

这篇关于如何使用 jQuery 触发对链接的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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