2014-03-18 195 views
1

我有一個程度的工作腳本。問題是最後一個內容沒有加載到目標div區域,但它加載到整個頁面。在加載頁面加載jQuery內容到div

第一頁是登陸頁面,並罰款:

<div class="container"> 
    <div class="row"> 
     <div class="col-md-2"> 
      <ul class="nav nav-pills nav-stacked"> 
       <li><a class="dashnav" href="content/company.html">Company</a> 
       </li> 
       <li><a class="dashnav" href="content/buyer.html">Buyer</a> 
       </li> 
       <li><a class="dashnav" href="content/settings.html">Settings</a> 
       </li> 
      </ul> 
     </div> 
     <div class="col-md-10"> 
      <div id="content"></div> 
     </div> 
    </div> 
</div> 
<!-- /container --> 

這是jQuery的。它加載菜單罰款,但沒有加載上次內容到dashcontent格:

$(document).ready(function() { 
    // select where the content is to go and what content is be loaded 

    $('#content').load($('.dashnav:first').attr('href')); 

    //click button from first dashnav 
    $('.dashnav').click(function() { 

     //What Link has been clicked 
     var href = $(this).attr('href'); 

     //load content from that link 
     $('#content').load(href, function() { 

      //once the content is loaded 
      $('#dashcontent').load($('.companynav:first').attr('href')); 

      //click on link from newly loaded page 
      $('.companynav').click(function() { 

       //What Link has been clicked 
       var href = $(this).attr('href'); 

       //load that content 
       $('#dashcontent').load(href); 

      }); 

     }); 

     return false; //stops the page redirect 

    }); 

}); 

第二頁:

<ul class="nav nav-pills companynav"> 
    <li><a href="content/dashboard/basic.html">Basic</a> 
    </li> 
    <li><a href="content/dashboard/factory.html">Factory</a> 
    </li> 
    <li><a href="content/dashboard/company_bio.html">Company Bio</a> 
    </li> 
</ul> 
<div class="col-md-10"> 
    <div id="dashcontent"></div> 
</div> 

最終結果應該在basic.html加載到dashcontent區但它不:

<h1>Basic Profile</h1> 
<p>Edit your Company profile</p> 

任何幫助將是偉大的。

+0

嘗試加入一些斷點或使用的console.log()來查看哪個回調正在運行。從我理解'$('#dashcontent')。load($('。companynav:first')。attr('href'));'失敗或永遠不會運行。那是對的嗎? – Mathias

回答

0

,請返回也是假的在你的第二個(嵌套)點擊處理程序,對.companynav:

.... 
//click on link from newly loaded page 
$('.companynav').click(function(){ 

    //What Link has been clicked 
    var href = $(this).attr('href'); 

    //load that content 
    $('#dashcontent').load(href); 

    return false; 

}); 
....