简体   繁体   English

jQuery单击无法始终触发

[英]jQuery click does not fire consistently

I have an issue regarding the jQuery click event. 我有一个关于jQuery click事件的问题。 The first time the page get loaded it does not work. 页面第一次加载时不起作用。 After a refresh it works fine. 刷新后,它可以正常工作。 I assume it has to do with the browser caching the files somehow. 我认为这与浏览器以某种方式缓存文件有关。

This is the code: 这是代码:

$(window).ready(
    function() {

        $("#language-input").click(function() {
            $("#language-dropdown").show();
        });

Any ideas what I am missing? 有什么想法我想念的吗?

Instead of 代替

$("#language-input").click(function() {

You can use 您可以使用

$("#language-input").on('click', function() { . $("#language-input").on('click', function() {

This will ensure that the click event is fired even if the element is loaded dynamically. 这将确保即使动态加载元素也会触发click事件。

You final code would be without $(window).ready(function() { : 您的最终代码将没有$(window).ready(function() {

$("#language-input").on('click', function() {
    $("#language-dropdown").show();
});

The solution, which was mentioned in one of the comments, was to use: 其中一项评论中提到的解决方案是使用:

$(document).on("click", "#element", function()

This seemed to work for dynamically added elements. 这似乎适用于动态添加的元素。

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

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