简体   繁体   中英

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. 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. And no need to append a script after body tag. You can put is anywhere in body element. For more info see documentation here: 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.

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> .

For a belt-and-braces approach do also use $(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: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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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