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

查看:178
本文介绍了如何使用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 =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');

无需调用find。 :)

No need to call find. :)

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

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