简体   繁体   English

页面内容完全加载后执行Jquery功能

[英]perform Jquery function after page content loads completely

i have a script that i want to be performed after the whole page has loaded completely not while page is loading. 我有一个脚本,我希望在整个页面完全加载后而不是在页面加载时执行。 I need you help. 我需要你的帮助。 below is the script. 下面是脚本。

jQuery(function () {
var $els = $('div[id^=quote]'),
    i = 0,
    len = $els.length;

$els.slice(1).hide();
setInterval(function () {
    $els.eq(i).fadeOut(function () {
        i = (i + 1) % len
        $els.eq(i).fadeIn(750);
    })
}, 3750)
})

Try: 尝试:

$(window).on("load", function () {
   // your code here
});

See explanation here : jQuery - What are differences between $(document).ready and $(window).load? 请参阅此处的说明: jQuery-$(document).ready和$(window).load有什么区别?

Use $( document ).ready(); 使用$( document ).ready();

This should work for you: 这应该为您工作:

$( document ).ready(function () {
var $els = $('div[id^=quote]'),
    i = 0,
    len = $els.length;

$els.slice(1).hide();
setInterval(function () {
    $els.eq(i).fadeOut(function () {
        i = (i + 1) % len
        $els.eq(i).fadeIn(750);
    })
}, 3750)
});

Your code appears to be correct, your just using a different namespace for jQuery / alias... 您的代码似乎是正确的,您只是对jQuery / alias使用了不同的命名空间...

$(function() {
  // DOM Ready Event..
  // Your already doing this
});

What is DOM Ready?? 什么是DOM Ready? well basically it waits for all the elements on your page to be created/ready.. This does not include certain things like images loading or other events.. In general, It means that you should be able to access any element at this point. 基本上,它会等待页面上的所有元素都已创建/就绪。。这不包括图像加载或其他事件之类的某些事情。通常,这意味着您应该可以在此时访问任何元素。

If you can provide a jsFiddle of your code or explain further what the actual issue is as such then we can help. 如果您可以提供代码的jsFiddle或进一步解释实际的问题所在,那么我们可以为您提供帮助。 But my guess is that your actually looking more for an event against certain elements.. 但是我的猜测是,您实际上正在寻找针对某些元素的事件。

Otherwise use as @NenadP suggested, this should load after the DOM ready even and generally will cover most things.. But just keep in mind if you have certain elements or frameworks/libraries.. You might need to look at their events/callbacks. 否则,按照@NenadP的建议使用,它应该在DOM准备就绪后加载,并且通常将涵盖大多数内容。.但是请记住,如果您具有某些元素或框架/库。。您可能需要查看它们的事件/回调。

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

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