2011-08-30 172 views
0

我目前正在爲客戶構建一個應用程序,並且遇到了問題。我目前正在使用腳本從當前文件本身的另一個文件加載內容,但是當我刷新時內容不會保留,但當我來回切換時,它也不會保存。下面是代碼:Jquery/Ajax內容加載刷新問題

$(document).ready(function() { 

var hash = window.location.hash.substr(1); 
var href = $('#accordionWrapper a').each(function(){ 
    var href = $(this).attr('href'); 
    if(hash==href.substr(0,href.length-5)){ 
     var toLoad = hash+'.jsp #content'; 
     $('#content').load(toLoad); 
    }           
}); 

$('#accordionWrapper a').live('click', function(){ 

    var toLoad = $(this).attr('href')+' #content'; 
    $('#content').hide('fast',loadContent); 
    $('#load').remove(); 
    $('#bodyWrapper').append('<span id="load">LOADING...</span>'); 
    $('#load').fadeIn('normal'); 
    window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5); 
    function loadContent() { 
     $('#content').load(toLoad,'',showNewContent()); 
    } 
    function showNewContent() { 
     $('#content').show('normal',hideLoader()); 
    } 
    function hideLoader() { 
     $('#load').fadeOut('normal'); 
    } 
    return false; 

}); 

}); 

我附上我的其他js文件的情況下有可能會發生衝突:

// Accordion Menu Function 
$(document).ready(function() { 
//ACCORDION BUTTON ACTION 
$('a.accordionButton').live('click', function() { 
    // Closes the current open ul. 
    $('ul.accordionContent').slideUp('normal'); 
    // opens the selected link's ul. 
    $(this).next().slideDown('normal'); 
    // removes the active class from all links. 
    $('a.accordionButton').each(function(){ 
     $(this).removeClass('selected'); 
    }); 
    // adds the active class to the selected link. 
    $(this).addClass('selected'); 
}); 

//ACCORDION BUTTON HOVER EFFECT 
$(".accordionButton").hover(
     function() { 
      $("span", this).stop().animate({ paddingRight: "30px" }, 200); 
     }, 
     function() { 
      $("span", this).stop().animate({ paddingRight: "0px" }); 
     } 
    ); 

//SIDEBAR MENU HIDE TOGGLE 
$('.hideSideBar').live('click', function() { 
     $('#sidebar').toggle("slide", function() { 
     }); 
}); 

$('.showSideBar').click(function() { 
    $('#sidebar').toggle('slide', function() { 
    }); 
}); 

//HIDE THE DIVS ON PAGE LOAD  
$("div.accordionContent").hide(); 
}); 


//REFRESH PAGE FIX 
function getCurrentPage() { 
//Defines the Path Variable to be the url's # tag 
var path = window.location.hash; 
//Tries every a class named accordionButton 
$('a.accordionButton').each(function(){ 
    //Loops whether the path is equal to the attributes id tag 
    if (path == $(this).attr('id')) { 
     //Adds class 'selected' to matching element. 
     $(this).addClass('selected'); 
     //Opens the selected link's ul. 
     $(this).next().slideDown('normal'); 
     /* 
     //Tries every a within an li child of accordionContent. 
     $('.accordionContent li a').each(function(){ 
      //Loops to find an element that's id equals the path variable. 
      if (path == $(this).attr('id')) { 
       //Adds class 'active' to matching element. 
       $(this).addClass('active'); 
      } 
     }); 
     */ 
    } 
}); 

} 

終於HTML:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script> 
<script type="text/javascript" src="js/contentLoader.js"></script> 
<script type="text/javascript" src="js/jquery-ui-1.8.16.js"></script> 
<script type="text/javascript" src="js/accordionMenu.js"></script> 
<link type="text/css" rel="stylesheet" href="css/accordionMenu.css" /> 
<link type="text/css" rel="stylesheet" href="css/style.css" /> 
</head> 
<body onload="getCurrentPage()"> 
<div id="bodyWrapper"> 
<div class="showSideBar" style="display:none;">Show Navigation [+]</div> 
<div id="sidebar"> 
    <a class="logo" href="dashboard.jsp"></a> 
    <div id="loginWelcome"> 
     <span>Welcome back</span> 
     <span class="username">John Doe</span> 
     <span class="logout"><a href="">Logout</a></span> 
    </div> 
    <div id="accordionWrapper"> 
     <a class="accordionButton" href="dashboard.jsp" id="#dashboar"><span>Dashboard</span></a> 
      <ul class="accordionContent"> 
       <li><a href="dashboard.jsp" class="active">Edit My Profile</a></li> 
      </ul> 
     <a class="accordionButton" href="workorders.jsp" id="#workorder"><span>Work Orders</span></a> 
      <ul class="accordionContent"> 
       <li><a href="">Create New Work Order</a></li> 
       <li><a href="workorders.jsp" class="active">Manage Work Orders</a></li> 
      </ul> 
     <a class="accordionButton" href="workitems.jsp" id="#workitem"><span>Work Items</span></a> 
      <ul class="accordionContent"> 
       <li><a href="">Create New Work Item</a></li> 
       <li><a href="workitems.jsp" class="active">Manage Work Items</a></li> 
      </ul> 
     <a class="accordionButton" href="users.jsp" id="#user"><span>Users</span></a> 
      <ul class="accordionContent"> 
       <li><a href="">Add New User</a></li> 
       <li><a href="users.jsp" class="active">Manage Users</a></li> 
      </ul> 
     <a class="accordionButton" href="airplanes.jsp" id="#airplane"><span>Airplanes</span></a> 
      <ul class="accordionContent"> 
       <li><a href="">Add New Airplane</a></li> 
       <li><a href="airplanes.jsp" class="active">Manage Airplanes</a></li> 
      </ul> 
     <a class="accordionButton" href="support.jsp" id="#suppor"><span>Support</span></a> 
    </div> 
    <a class="hideSideBar" href="#">Hide Navigation [-]</a> 
</div> 
<div id="contentWrapper"> 
    <div id="content"> 

    </div> 
</div> 

回答

0

所以,你想要什麼發生的事情是,即使在頁面重新加載後,加載的內容仍然可見?

如果是這種情況,您將不得不使用cookie(或某種存儲系統)來跟蹤最後加載的內容是什麼,因爲內容是被動態加載的