触发锚标记上的点击事件不起作用

时间:2022-12-09 21:49:38

I just ran into this question

我刚碰到这个问题

FIDDLE

Triggering click event on anchor tag is not working here.

触发锚标记上的点击事件不起作用。

<a class="button2" href="#popup1">hello</a><div id="popup1" class="overlay">  <div class="popup">    <div class="contentSpec">      <h2>Would you like to visit</h2>      <h1>someURL</h1>    </div>    <a class="close" href="#"></a>    <div class="content">      <div class="box">        <a class="button" href="#popup1">YES</a>        <a class="button1" href="#popup1">NO</a>      </div>    </div>  </div></div>

JS:

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

My question, is why the trigger event is not working in this case?

我的问题是,为什么触发事件在这种情况下不起作用?

3 个解决方案

#1


16  

You need to call native DOM click() method in order to fire default clicking anchor behaviour, jQuery specifically excludes it on anchor:

你需要调用本机DOM click()方法来触发默认点击锚行为,jQuery明确地将其排除在锚点上:

$(document).ready(function() {  $(".button2")[0].click();});

-jsFiddle-

#2


4  

Use

$(".button2").get(0).click();

The get(0) will return the first DOM object instead of the jquery object, and click() will be triggered.

get(0)将返回第一个DOM对象而不是jquery对象,并且将触发click()。

Updated fiddle

#3


1  

As you don't have any .click() event bound on it, it never fires it.

由于您没有绑定任何.click()事件,因此它永远不会触发它。

You need to fire the DOM click event with .click() instead of .trigger(e) of jQuery method and this should only work on dom nodes. Which you can achieve by introducing the index [0] or with jQuery's method .get(0).

您需要使用.click()而不是jQuery方法的.trigger(e)来激活DOM click事件,这应该只适用于dom节点。您可以通过引入索引[0]或使用jQuery的方法.get(0)来实现。

Instead try this:

相反,试试这个:

$(document).ready(function() {  $(".button2")[0].click();  // or $(".button2").get(0).click();});

and if this is the case then you can do this with javascript only:

如果是这种情况,那么你只能用javascript做到这一点:

window.addEventListener('DOMContentLoaded', function(e){   document.querySelector('.button2').click();}, false);

#1


16  

You need to call native DOM click() method in order to fire default clicking anchor behaviour, jQuery specifically excludes it on anchor:

你需要调用本机DOM click()方法来触发默认点击锚行为,jQuery明确地将其排除在锚点上:

$(document).ready(function() {  $(".button2")[0].click();});

-jsFiddle-

#2


4  

Use

$(".button2").get(0).click();

The get(0) will return the first DOM object instead of the jquery object, and click() will be triggered.

get(0)将返回第一个DOM对象而不是jquery对象,并且将触发click()。

Updated fiddle

#3


1  

As you don't have any .click() event bound on it, it never fires it.

由于您没有绑定任何.click()事件,因此它永远不会触发它。

You need to fire the DOM click event with .click() instead of .trigger(e) of jQuery method and this should only work on dom nodes. Which you can achieve by introducing the index [0] or with jQuery's method .get(0).

您需要使用.click()而不是jQuery方法的.trigger(e)来激活DOM click事件,这应该只适用于dom节点。您可以通过引入索引[0]或使用jQuery的方法.get(0)来实现。

Instead try this:

相反,试试这个:

$(document).ready(function() {  $(".button2")[0].click();  // or $(".button2").get(0).click();});

and if this is the case then you can do this with javascript only:

如果是这种情况,那么你只能用javascript做到这一点:

window.addEventListener('DOMContentLoaded', function(e){   document.querySelector('.button2').click();}, false);