在链接上模拟jQuery / JavaScript中的单击

时间:2022-11-21 15:36:49

I want to simulate a click on any link on a page using JavaScript. If that link has some function binded to its 'onclick' event (by any other JS I don't have any control over), then that function must be called otherwise the link should behave in the normal manner and open a new page.

我想使用JavaScript模拟页面上任何链接的点击。如果该链接有一些绑定到其'onclick'事件的函数(由任何其他JS我没有任何控制权),则必须调用该函数,否则链接应以正常方式运行并打开新页面。

I am not sure that just checking the value of the 'onclick' handler would suffice. I want to build this so that it works on any link element.

我不确定只检查'onclick'处理程序的值就足够了。我想构建它,以便它适用于任何链接元素。

I have no control over what function maybe binded to the onclick event of the link using whichever JS library (not necessarily jQuery) or by simply using JavaScript.

我无法使用任何JS库(不一定是jQuery)或简单地使用JavaScript来控制可能绑定到链接的onclick事件的函数。

EDIT: With the help of the answers below, it looks like it is possible to check for event handlers attached using jQuery or using the onclick attribute. How do I check for event handlers attached using addEventListener / any other JS library so that it is foolproof?

编辑:在下面的答案的帮助下,看起来可以检查使用jQuery附加的事件处理程序或使用onclick属性。如何检查使用addEventListener /任何其他JS库附加的事件处理程序,以便它是万无一失的?

7 个解决方案

#1


42  

You can use the the click function to trigger the click event on the selected element.

您可以使用单击功能触发所选元素上的单击事件。

Example:

例:

$( 'selector for your link' ).click ();

You can learn about various selectors in jQuery's documentation.

您可以在jQuery的文档中了解各种选择器。

EDIT: like the commenters below have said; this only works on events attached with jQuery, inline or in the style of "element.onclick". It does not work with addEventListener, and it will not follow the link if no event handlers are defined. You could solve this with something like this:

编辑:像下面的评论者说的那样;这仅适用于使用jQuery,内联或“element.onclick”样式附加的事件。它不适用于addEventListener,如果没有定义事件处理程序,它将不会跟随链接。你可以用这样的方法解决这个问题:

var linkEl = $( 'link selector' );
if ( linkEl.attr ( 'onclick' ) === undefined ) {
    document.location = linkEl.attr ( 'href' );
} else {
    linkEl.click ();
}

Don't know about addEventListener though.

但是不知道addEventListener。

#2


38  

Why not just the good ol' javascript?

为什么不只是好的'javascript?

$('#element')[0].click()

$( '#元件')[0​​]。点击()

#3


27  

Just

只是

$("#your_item").trigger("click");

using .trigger() you can simulate many type of events, just passing it as the parameter.

使用.trigger()可以模拟许多类型的事件,只需将其作为参数传递即可。

#4


8  

Easy! Just use jQuery's click function:

简单!只需使用jQuery的点击功能:

$("#theElement").click();

#5


7  

Try this

尝试这个

function submitRequest(buttonId) {
    if (document.getElementById(buttonId) == null
            || document.getElementById(buttonId) == undefined) {
        return;
    }
    if (document.getElementById(buttonId).dispatchEvent) {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        document.getElementById(buttonId).dispatchEvent(e);
    } else {
        document.getElementById(buttonId).click();
    }
}

and you can use it like

你可以像使用它一样

submitRequest("target-element-id");

#6


1  

At first see this question to see how you can find if a link has a jQuery handler assigned to it.

首先看到这个问题,看看如何找到一个链接分配给它的jQuery处理程序。

Next use:

下次使用:

$("a").attr("onclick")

to see if there is a javascript event assigned to it.

查看是否有分配给它的javascript事件。

If any of the above is true, then call the click method. If not, get the link:

如果以上任何一种情况属实,则调用click方法。如果没有,请获取链接:

$("a").attr("href")

and follow it.

并遵循它。

I am afraid I don't know what to do if addEventListener is used to add an event handler. If you are in charge of the full page source, use only jQuery event handlers.

如果使用addEventListener添加事件处理程序,我恐怕不知道该怎么办。如果您负责整页源代码,请仅使用jQuery事件处理程序。

#7


0  

All this might not help say when you use rails remote form button to simulate click to. I tried to port nice event simulation from prototype here: my snippets. Just did it and it works for me.

当您使用rails远程表单按钮模拟单击时,所有这些可能没有帮助。我试图在这里从原型移植好的事件模拟:我的片段。做到了,它对我有用。

#1


42  

You can use the the click function to trigger the click event on the selected element.

您可以使用单击功能触发所选元素上的单击事件。

Example:

例:

$( 'selector for your link' ).click ();

You can learn about various selectors in jQuery's documentation.

您可以在jQuery的文档中了解各种选择器。

EDIT: like the commenters below have said; this only works on events attached with jQuery, inline or in the style of "element.onclick". It does not work with addEventListener, and it will not follow the link if no event handlers are defined. You could solve this with something like this:

编辑:像下面的评论者说的那样;这仅适用于使用jQuery,内联或“element.onclick”样式附加的事件。它不适用于addEventListener,如果没有定义事件处理程序,它将不会跟随链接。你可以用这样的方法解决这个问题:

var linkEl = $( 'link selector' );
if ( linkEl.attr ( 'onclick' ) === undefined ) {
    document.location = linkEl.attr ( 'href' );
} else {
    linkEl.click ();
}

Don't know about addEventListener though.

但是不知道addEventListener。

#2


38  

Why not just the good ol' javascript?

为什么不只是好的'javascript?

$('#element')[0].click()

$( '#元件')[0​​]。点击()

#3


27  

Just

只是

$("#your_item").trigger("click");

using .trigger() you can simulate many type of events, just passing it as the parameter.

使用.trigger()可以模拟许多类型的事件,只需将其作为参数传递即可。

#4


8  

Easy! Just use jQuery's click function:

简单!只需使用jQuery的点击功能:

$("#theElement").click();

#5


7  

Try this

尝试这个

function submitRequest(buttonId) {
    if (document.getElementById(buttonId) == null
            || document.getElementById(buttonId) == undefined) {
        return;
    }
    if (document.getElementById(buttonId).dispatchEvent) {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        document.getElementById(buttonId).dispatchEvent(e);
    } else {
        document.getElementById(buttonId).click();
    }
}

and you can use it like

你可以像使用它一样

submitRequest("target-element-id");

#6


1  

At first see this question to see how you can find if a link has a jQuery handler assigned to it.

首先看到这个问题,看看如何找到一个链接分配给它的jQuery处理程序。

Next use:

下次使用:

$("a").attr("onclick")

to see if there is a javascript event assigned to it.

查看是否有分配给它的javascript事件。

If any of the above is true, then call the click method. If not, get the link:

如果以上任何一种情况属实,则调用click方法。如果没有,请获取链接:

$("a").attr("href")

and follow it.

并遵循它。

I am afraid I don't know what to do if addEventListener is used to add an event handler. If you are in charge of the full page source, use only jQuery event handlers.

如果使用addEventListener添加事件处理程序,我恐怕不知道该怎么办。如果您负责整页源代码,请仅使用jQuery事件处理程序。

#7


0  

All this might not help say when you use rails remote form button to simulate click to. I tried to port nice event simulation from prototype here: my snippets. Just did it and it works for me.

当您使用rails远程表单按钮模拟单击时,所有这些可能没有帮助。我试图在这里从原型移植好的事件模拟:我的片段。做到了,它对我有用。