单击与JQuery链接时出现意外结果

时间:2022-08-26 19:58:24

I don't know if this is just a simple oversight on my end, but what am I doing wrong? I would like the results of regular.js, but why isn't this working in jquery?

我不知道这对我来说是否只是一个简单的疏忽,但我做错了什么?我想看一下regular.js的结果,但为什么这不适用于jquery?

html:
<a href="" class="foobar" id="foobar">click me</a>

regular.js:
document.getElementById('foobar').click();
// this results in the page continuously clicking and reloading the page


jquery.js:
$(document).ready(function() {
    setTimeout(function() {
        $("#foobar").trigger('click');
    },5);
});
// this waits for page to load before executing, but does nothing.

2 个解决方案

#1


0  

Not entirely sure why yours is not working - working example in jsfiddle

不完全确定为什么你的工作不起作用 - 在jsfiddle中的工作示例

$(document).ready(function() {

    $(document).on('click',"#foobar",function(){alert("hello world")})

    setTimeout(function() {
        $("#foobar").trigger('click');
    },5);
});

#2


0  

From jQuery docs

来自jQuery文档

Trigger: Execute all handlers and behaviors attached to the matched elements for the given event type.

触发器:执行附加到给定事件类型的匹配元素的所有处理程序和行为。

You should replace .trigger('click') with .click()

你应该用.click()替换.trigger('click')


Update

After reading the comments and searching some more, I found the following:

阅读评论并搜索更多内容后,我发现了以下内容:

The problem

jQuery can activate methods bound using:

jQuery可以激活绑定方法:

  • jQuery (.bind, .on, .click etc)
  • jQuery(.bind,.on,.click等)

  • native binding element.onclick = function() {}.
  • native binding element.onclick = function(){}。

But if a method was added using element.addEventListener(), jQuery's .click() and .trigger() will fail silently.

但是如果使用element.addEventListener()添加了一个方法,则jQuery的.click()和.trigger()将以静默方式失败。

The solution

Using element.click() activates all bound methods. So does a real click from mouse, but that is trivial.

使用element.click()激活所有绑定方法。鼠标的实际点击也是如此,但这是微不足道的。

Also, MDN states, and the OP has pointed out, the following:

另外,MDN指出,OP已经指出了以下内容:

The click() method will not cause an element to initiate navigation as if a real mouse-click had been received.

click()方法不会导致元素启动导航,就像收到了真正的鼠标单击一样。

I have tried in Chrome 52 and Firefox 48, on Ubuntu 14, and it seems to work for me. Still, unsure if what MDN states is false.

我在Ubuntu 14上试过Chrome 52和Firefox 48,它似乎对我有用。不过,不确定MDN所说的是错误的。

#1


0  

Not entirely sure why yours is not working - working example in jsfiddle

不完全确定为什么你的工作不起作用 - 在jsfiddle中的工作示例

$(document).ready(function() {

    $(document).on('click',"#foobar",function(){alert("hello world")})

    setTimeout(function() {
        $("#foobar").trigger('click');
    },5);
});

#2


0  

From jQuery docs

来自jQuery文档

Trigger: Execute all handlers and behaviors attached to the matched elements for the given event type.

触发器:执行附加到给定事件类型的匹配元素的所有处理程序和行为。

You should replace .trigger('click') with .click()

你应该用.click()替换.trigger('click')


Update

After reading the comments and searching some more, I found the following:

阅读评论并搜索更多内容后,我发现了以下内容:

The problem

jQuery can activate methods bound using:

jQuery可以激活绑定方法:

  • jQuery (.bind, .on, .click etc)
  • jQuery(.bind,.on,.click等)

  • native binding element.onclick = function() {}.
  • native binding element.onclick = function(){}。

But if a method was added using element.addEventListener(), jQuery's .click() and .trigger() will fail silently.

但是如果使用element.addEventListener()添加了一个方法,则jQuery的.click()和.trigger()将以静默方式失败。

The solution

Using element.click() activates all bound methods. So does a real click from mouse, but that is trivial.

使用element.click()激活所有绑定方法。鼠标的实际点击也是如此,但这是微不足道的。

Also, MDN states, and the OP has pointed out, the following:

另外,MDN指出,OP已经指出了以下内容:

The click() method will not cause an element to initiate navigation as if a real mouse-click had been received.

click()方法不会导致元素启动导航,就像收到了真正的鼠标单击一样。

I have tried in Chrome 52 and Firefox 48, on Ubuntu 14, and it seems to work for me. Still, unsure if what MDN states is false.

我在Ubuntu 14上试过Chrome 52和Firefox 48,它似乎对我有用。不过,不确定MDN所说的是错误的。