jQuery jquery点击对超链接不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2053598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:40:02  来源:igfitidea点击:

jquery click doesn't work on hyperlink

jqueryclickhyperlink

提问by Sean

I have a simple link click simulation that I want to do using jQuery. From what I read, this should work, but the code below doesn't work. Nothing happens if I do the same thing as a part of some other event or something either. Thoughts?

我有一个简单的链接点击模拟,我想使​​用 jQuery 进行。从我读到的,这应该有效,但下面的代码不起作用。如果我作为其他事件的一部分或某事的一部分做同样的事情,则不会发生任何事情。想法?

<script type="text/javascript">
  $(function() {
    $("#lnk_0").click();
  });
</script>

<a id="lnk_0" href="http://mydomain.com/mypage.html">link</a>

回答by cletus

See click():

click()

Triggers the click event of each matched element.

Causes all of the functions that have been bound to that click event to be executed.

触发每个匹配元素的点击事件。

导致所有绑定到该点击事件的函数被执行。

The important thing to note is that it does notduplicate clicking the link. It only triggers associated events. If you want to change location:

需要注意的重要一点是,它并没有重复点击的链接。它只触发相关事件。如果要更改位置:

var link = $("#link_0");
link.click();
window.location.href = link.attr("href");

but even that is only an approximation as it doesn't cater for handlers stopping event propagation.

但即使这只是一个近似值,因为它不适合停止事件传播的处理程序。

回答by SLaks

Calling jQuery's clickmethod will invoke any clickhandlers that you've added, but will not click the link.

调用 jQuery 的click方法将调用click您添加的任何处理程序,但不会单击链接。

You need to write:

你需要写:

window.location = $("#lnk_0").attr('href');

(This assumes that there aren't any clickevent handlers)

(这假设没有任何click事件处理程序)



EDIT: In response to your comment, you can call the IE-only DOM clickmethod, like this:

编辑:为了回应您的评论,您可以调用 IE-only DOMclick方法,如下所示:

if ($("#lnk_0")[0].click)
    $("#lnk_0")[0].click();
else
    window.location = $("#lnk_0").attr('href');

回答by Spell

To allow the hyperlink functionality (hover, hand cursor, etc) when overriding with jQuery you can do the following:

要在使用 jQuery 覆盖时允许超链接功能(悬停、手形光标等),您可以执行以下操作:

<script>
    $(function() {
        $( "#dialog-modal" ).dialog({
            autoOpen: false,
            height: 320,
            modal: true
        });

        $( "#open-modal" ).click(function() {
            $( "#dialog-modal" ).dialog( "open" );
        });         
</script>
<a href="javascript:return true" id="open-modal">Open Modal</a>

<div id="dialog-modal" title="Hello World">
Hello
</div>

回答by Ricardo Green

Doing this works:

这样做有效:

$('#lnk_0')[0].click();

回答by James k

I am not sure it solves your precise problem, but here is a snippet of code I use to simulate a user click:

我不确定它是否能解决您的确切问题,但这是我用来模拟用户点击的一段代码:

var target = "...";

var e = jQuery.Event("click");

target.trigger(e);

if (!e.isDefaultPrevented() && target.attr("href"))
    window.location.href = target.attr('href');