我是jquery/javascript的新手,我想做一个回调函数来重新加载jquery插件。怎样做才是合适的呢?
$(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');
});
我以前使用过这个插件,我所做的是在加载函数中添加我的代码
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");
});
});
}
此外,如果你使用CSS-Tricks方法,那么任何jquery插件应用的页面被加载应该已经执行。
就像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