简体   繁体   English

点击加载所有“ a” HTML元素

[英]Loading on click on all “a” HTML elements

I want to show loader when someone clicks on any element on my site. 当有人点击我网站上的任何元素时,我想显示加载程序。

My Code: 我的代码:

(function ($) {
    $('body').append("<div id='loader-box'></div>");
    $("loader-box").html("");
    $("a").on('click', function (e) {
        $("loader-box").html("<div class='loader-wrapper'><div class='loader-image'></div></div>");
    });
}(jQuery));

The problem is that append does not work. 问题是追加无法正常工作。 I do not understand why. 我不理解为什么。 Thanks! 谢谢!

--- UPDATE --- -更新-

I found a solution, thanks to all especially Andrei Cristian Prodan who helped me to solve the problem with .on() function. 我找到了一个解决方案,这要感谢所有特别是Andrei Cristian Prodan的人,他们帮助我使用.on()函数解决了问题。 I also use the wrong function to add "loader-box" div. 我还使用了错误的函数来添加“ loader-box” div。

This is my code: 这是我的代码:

(function($){
    $("body").prepend('<div id="loader-box"></div>');
    $("#loader-box").html("");
    $("html").on("click", "a",function(){
        $("#loader-box").html('<div class="loader-wrapper"><div class="loader-image"></div></div>');
    });
}(jQuery));

Thanks! 谢谢!

You are missing a "#" in your selector $("#loader-box"); 您在选择器$(“#loader-box”)中缺少“#”;

Also if you are executing this code before the anchors exist in your page (for eg at the top of your body or inside the header), the click will not be assigned because $('a') will be empty. 同样,如果您在页面中的锚点存在之前(例如,在您的正文顶部或标题中)执行此代码,则不会分配该点击,因为$('a')为空。

Do this instead: $('html').on('click', 'a', function() {...} ); 而是这样做: $('html').on('click', 'a', function() {...} );

Your selectors are just a little off; 您的选择器只差一点了。 missing #: 失踪 #:

<script>
$(function($){
    $('body').append("<div id='loader-box'></div>");
        $("#loader-box").html("");
        $("a").on('click', function(e){
            $("#loader-box").html("<div class='loader-wrapper'><div class='loader-image'></div></div>");
        });
});
</script>

And DOM ready needs to be written correctly. DOM准备就绪,需要正确编写。

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

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