JQuery wrapInner 触发器 jQuery(document).ready 两次.

JQuery wrapInner triggers jQuery(document).ready twice

本文关键字:ready 两次 document wrapInner 触发器 jQuery JQuery      更新时间:2023-09-26

我正在使用wrapInner函数,但这些函数再次触发了我的jQuery(document).ready事件。

这是正常行为吗?如何避免这种情况?

更新:

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'});
                    });
                }
            });
        }

如果我取消注释第 4 行,则会显示两次以下警报。注释该行后,警报仅显示一次。

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

由于您的代码位于正文的子元素中,一旦您将正文的内容包装在另一个div 中,将重新执行正文中的脚本。只需在使用 wrapInner 之前删除这些脚本即可。删除脚本不会影响页面的功能,除了不会导致警报发生两次。

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