简体   繁体   English

jQuery插件在动态加载的html中不起作用

[英]Jquery plugins won't work in dynamically loaded html

i got this code to dynamically load html BUT i got a problem cause my plugins work fine on index.html but when you change page it no longer works..anyone who can help me out? 我有这段代码可以动态加载html但我有问题,原因是我的插件在index.html上工作正常,但是当您更改页面时,它不再起作用..有人可以帮助我吗? thnx n

$(function() { $(function(){

var newHash      = "",
    $mainContent = $("#main-content"),
    $pageWrap    = $("#page-wrap"),
    baseHeight   = 0,
    $el;


$("nav").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    return false;
});

$(window).bind('hashchange', function(){

    newHash = window.location.hash.substring(1);

    if (newHash) {
        $mainContent
            .find("#guts")
            .fadeOut(200, function() {
                $mainContent.hide().load(newHash + " #guts", function() {
                    $mainContent.fadeIn(200, function() {
                        $pageWrap.animate({
                            height: baseHeight + $mainContent.height() + "px"
                        });
                    });
                    $("nav a").removeClass("current");
                    $("nav a[href="+newHash+"]").addClass("current");
                });
            });


    };

});

$(window).trigger('hashchange');

}); });

I think what you need is .live() if you want to attached jQuery to dynamically created markup. 我想,如果您想将jQuery附加到动态创建的标记中,则需要的是.live()。

http://api.jquery.com/live/ http://api.jquery.com/live/

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

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