2016-09-28 66 views
0

我想要有HTML菜單,用戶可以點擊,然後頁面更改內容。它像用戶的個人資料頁面中,用戶列表一樣賬戶,結算,圖片等,用戶配置文件的側邊菜單和基於點擊更改內容

<li><a href="" data-box="index"><i class="fa fa-user"></i> My Profile</a></li> 
<li><a href="" data-box="account"><i class="fa fa-edit"></i> Edit Profile</a></li> 
<div class="box index"> 
    @include("partials.profile_index") 
    </div> 
    <div class="box account"> 
    @include("partials.profile_account") 
    </div> 
<script> 

$(document).ready(function(){ 

$('a').click(function(ev){ 
    ev.preventDefault(); 

    // hide everything 
    $('.box').not(
     // except the corresponding box 
     $('.box.'+$(this).data('box')+':hidden').fadeIn() 
    ).fadeOut(); 
}); 

}); 


</script> 

回答

0

您可以按照以下方式做到這一點。檢查演示 - Fiddle Demo

$(document).ready(function(){ 

    $('a').click(function(ev){ 
     ev.preventDefault(); 

     // hide everything 
     var $thisBox = $('.box.' + $(this).data('box')); 
     $('.box').not($thisBox).fadeOut(); 
     if ($thisBox.is(':hidden')) { 
      $thisBox.fadeIn(); 
     } 
    }); 

}); 
+0

只有第一個鏈接工作.. sceond不工作..也所有「a」點擊受到影響,我不想要 – Alzaabi98

+0

不知道你在說什麼。在我的答案演示中,兩個鏈接都正常工作。 –

+0

我被卡住了..我在左邊尋找個人資料按鈕,右邊的內容..內容應該帶上服務器的數據..例如..帳戶信息..照片...帳單信息..等等...我使用laravel作爲朗。 – Alzaabi98

1

我清理了一下你的代碼並添加了一些解釋說明。這應該對你有用,也有助於未來的調試。最後,我還確保在傳出框完成動畫後發生傳入的框動畫。 See this fiddle

$(document).ready(function(){ 

    // hide all boxes by default. 
    $('.box').hide(); 

    // when someone clicks a link, process hiding/showing the correct box 
    $('a').click(function(ev){ 

    // stop the default action for the click event. 
    ev.preventDefault(); 

    // assign a friendly variable for the current box 
    var currentBox = $(this).attr("data-box"); 

    // hide everything except for the box we want to show 
    $('.box').not('.' + currentBox).fadeOut('slow', function() { 

     // after the animation is complete fade in the box we want to show. 
     $('.box.' + currentBox).fadeIn('slow'); 

    }); 

    }); 

}); 
+0

仍然沒有工作:我得到了第一個工作:@include(「partials.profile_index」),但第二個不工作:@include(「partials.profile_account」) 不知道爲什麼....也當我點擊在其他「a」href它也disapear – Alzaabi98

+0

@ Alzaabi98你的代碼仍然不工作或是小提琴不工作?小提琴在我的瀏覽器上爲我工作。如果這是你的代碼仍然無法使用,你可以用你當前版本的小提琴來更新你的帖子,這樣我可以看看嗎? –

+0

您的代碼工作正常..只有當我在我的代碼中使用它..它不起作用 – Alzaabi98

1

您寫道:「...單擊,然後頁面更改內容......」所以我會假設你希望瀏覽器重新加載頁面,而不是簡單地通過JavaScript更新DOM。如果我誤解了你的問題,請原諒。

我通常如何處理這個問題是給我的鏈接分配一個名爲「action」的URL變量。這樣當頁面重新加載時,我知道要顯示哪些內容。

根據您使用的服務器端語言,您的代碼可能會有所不同。我會CFML證明,但邏輯應該是一樣的,不管是否使用PHP等

這裏是你的鏈接可能外觀的示例:

<ul> 
    <li><a href="index.cfm?action=myProfile" data-box="index"><i class="fa fa-user"></i> My Profile</a></li> 
    <li><a href="index.cfm?action=editProfile" data-box="account"><i class="fa fa-edit"></i> Edit Profile</a></li> 
</ul> 

,那麼你會提供一些服務器端的邏輯是這樣的index.cfm頁面類型:

<!--- does the user want to view his/her profile? ---> 
<cfif action IS "myProfile"> 

    <div class="box index"> 
     @include("partials.profile_index") 
    </div> 

</cfif> 

<!--- does the user want to edit his/her profile? ---> 
<cfif action IS "editProfile"> 

    <div class="box index"> 
     @include("partials.profile_account") 
    </div> 

</cfif> 

有了這個解決方案,你不需要任何的JavaScript代碼。只需使用URL變量創建正常鏈接,即描述您希望目標頁面在加載時顯示的內容。

+0

你可能是對的..我試試這個邏輯 – Alzaabi98