简体   繁体   English

如何在body标签后附加jQuery脚本?

[英]How to append jquery script after a body tag?

I needed to put my script after the body tag to make sure that my script runs after all the elements are loaded. 我需要将脚本放在body标签之后,以确保在加载所有元素之后脚本可以运行。 I would greatly appreciate your help. 非常感谢您的帮助。

$(document).ready(function() {  
    $(document.body).prepend(
        console.log('test8'); 

        //custom jquery
        if ($('div.fc-bg table').length > 0) {     
            var holiday = ['2016-12-01','2016-12-23','2016-12-30'];
            $.each(holiday, function(i, val) {
                var date = val; //'2016-12-01';
                $('.fc-day-top[data-date='+date+']').addClass('holiday');        
            });     
        } //end of custom jQuery  
    )};
});

Thanks in advance. 提前致谢。

I will recommend you to use $(document).ready() event for purposes like this. 我建议您将$(document).ready()事件用于此类目的。 And no need to append a script after body tag. 并且无需在body标签之后附加脚本。 You can put is anywhere in body element. 您可以将其放置在body元素中的任何位置。 For more info see documentation here: https://learn.jquery.com/using-jquery-core/document-ready/ 有关更多信息,请参见此处的文档: https : //learn.jquery.com/using-jquery-core/document-ready/

I needed to put my script after the body tag to make sure that my script runs after all the elements are loaded. 我需要将脚本放在body标签之后,以确保在加载所有元素之后脚本可以运行。

To ensure that all the elements are loaded before your script runs, you need to put your <script>...</script> at the end of the document body, just before </body> . 为了确保在脚本运行之前加载所有元素,您需要将<script>...</script>放在文档正文的末尾,即</body>之前。

For a belt-and-braces approach do also use $(document).ready(); 对于带括号的方法,也请使用$(document).ready(); like so: 像这样:

<html>
    <head>
    [... HEAD CODE HERE...]
    </head>

    <body>
    [... BODY CODE HERE...]

    <script>
    $(document).ready(function(){

    [... JQUERY CODE HERE...]

    });
    </script>
    </body>
</html>

Use the eventAfterAllRender event 使用eventAfterAllRender事件

     eventAfterAllRender:function(){

 //custom jquery
    if($('div.fc-bg table').length > 0 ){     

         var holiday = ['2016-12-01','2016-12-23','2016-12-30'];
            $.each(holiday, function(i, val){
                var date = val;//'2016-12-01';
                 $(".fc-day-top[data-date="+date+"]").addClass('holiday');        

            });     

    } //end of custom jquery


        }

demo: http://codepen.io/anon/pen/ObqVwy 演示: http : //codepen.io/anon/pen/ObqVwy

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

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