jQuery触发器单击事件不适用于div [英] jQuery trigger click event not working on div
问题描述
我想触发div
上的点击事件.我使用以下代码,但无法正常工作.
I want to trigger a click event on a div
. I use the following code but it is not working.
$('#showbanner').trigger('click');
<div id="showbanner">show banner</div>
我做错了什么?我实际上想在页面加载2秒后显示横幅.在我添加延迟事件之前,此div的click事件没有触发.
What is that I am doing wrong? I actually want to show the banner 2 seconds after page is loaded. Before I could add delay event my click event for this div is not firing.
这是我的代码示例: http://codepen.io/anon/pen/aOzyxo
推荐答案
问题是因为您的trigger('click');
调用是脚本中的第一件事;它被称为 before .
The issue is because your trigger('click');
call is the first thing in your script; it's called before the event is attached.
您需要将其移动到脚本的末尾,以便在调用事件处理程序时将其绑定:
You need to move it to the end of the script so that the event handlers are bound when it is called:
这篇关于jQuery触发器单击事件不适用于div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!