简体   繁体   中英

function callback to reload a jquery plugin

im new to jquery/javascript and im trying to make a function callback to reload a jquery plugin. what would be the appropriate way to do that?

$(function() {

    var newHash      = "",
    $mainContent = $("#main-content"),
    $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() {
                        /*rebind plugin*/
                    });
                    $("nav a").removeClass("current");
                    $("nav a[href="+newHash+"]").addClass("current");
                });
            });
        };
    });    

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

I used this plugin before and the way i did was I added my code in the load function

if (newHash) {
        $mainContent
            .find("#guts")
            .fadeOut(200, function() {
                $mainContent.hide().load(newHash + " #guts", function() {
                    /* load in javascript you want to apply to the new content being loaded */
                    $mainContent.fadeIn(200, function() {
                    /*rebind plugin*/   
                    });
                    $("nav a").removeClass("current");
                    $("nav a[href="+newHash+"]").addClass("current");
                });
            });


    }

Also, if you use CSS-Tricks method then what ever jquery plugins applied from the page being loaded should already be executed.

like in the example provided from css-tricks

<html>
    <head>
        <script jquery.js file here>
    <body>
        <div id="wrap">
         // the stuff here will loaded to the page retrieving it already spiced up by jquery from the script in the head

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