简体   繁体   中英

JQuery wrapInner triggers jQuery(document).ready twice

I am using the wrapInner function but these triggers my jQuery(document).ready event once again.

Is this the normal behaviour? How can this be avoided?

Update:

miscellaneous : function(){
            $('#nav .active a').bind('click',function(){ return false });
            $('.type-a, .type-b, .type-c, .type-d, .type-e').append('<div class="type"></div>');
            //$('body').wrapInner('<div id="root"></div>');
            $('#content').wrap('<div id="content-wrapper"></div>');

            $('#filter .time > li').append('<span class="arrow"></span>');
            $('#filter .category li a').wrapInner('<span></span>');                 
            $('#filter .time > li > ul').hide();    
            $('#filter .time > li').live('mouseenter',function(){
                if($(this).children('ul').css('display') != 'block'){
                    $(this).children('ul').fadeIn('fast',function(){
                        $(this).css({'display':'block'});
                    });
                }
            }).live('mouseleave',function(){
                if($(this).children('ul').css('display') != 'none'){
                    $(this).children('ul').fadeOut('fast',function(){
                        $(this).css({'display':'none'});
                    });
                }
            });
        }

If I uncomment the 4th line the following alert is shown twice. With the line commented the alert is shown only once.

jQuery(document).ready(function($) {
    alert('in ready');              
    });

Since your code in a child element of the body, once you wrap the body's content with another div, scripts within the body will be re-executed. Simply remove those scripts before you use wrapInner. Removing the scripts will not affect the functionality of your page other than not causing the alert to happen twice.

$("body").find("script").remove().end().wrapInner("<div id='root' />");

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