简体   繁体   English

点击事件在页面首次加载时未触发

[英]Click event not firing the first time a page loads

I'm setting up a page with javascript that should fire on a click event. 我正在使用javascript设置页面,该页面应在click事件上触发。

It works most of the time except the first time a page loads. 它在大多数时间都有效,除了第一次加载页面。

If I refresh it though, it works and the click events fire properly. 如果我刷新它,它会起作用,并且单击事件会正确触发。

 $(document).ready(function() { 
     $(".element").click(function() { alert("This should work"); }
 });

Anybody have an idea where the issue might be? 有人知道问题可能在哪里吗?

if .element is class then it should probably be work fine. 如果.element是class,则应该可以正常工作。

But if you are trying to give actual element like <a></a> or <b></b> or <div></div> so : 但是,如果您尝试提供<a></a> or <b></b> or <div></div>类的实际元素,则:

 $(document).ready(function() { 
     $("div").click(function() { alert("This should work"); }                //Work on click on any div in page ( here is no . before div)
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM