2011-07-20 93 views
0

我的網站有兩個頁面都有超鏈接,但當用戶點擊該超鏈接時,我只需要該頁面的某個div替換爲其他頁面上的某個div,請指導我如何對其進行編程。使用標記替換div

結構

page1.html

​​

page2.html

<div id="no_replace"> 
    <a href="page1.html">page 1</a> 
    <div id="replace"> 
    //page two stuff 
    </div> 
</div> 
這樣一個當USR點擊鏈接頁 「兩頁」 上

...我想要的「取代'第一頁的div被'替換'page2.html的div替換,並且應該淡入和淡出

+0

[使用標籤來代替的div]的可能重複(http://stackoverflow.com/questions/6766563/using-a-tag-to-replace-嘗試此div) – Neal

回答

1

使用load()方法。

$('a').click(function(){ 
    $('div').load($(this).attr('href')); 
}); 
+0

fadein淡出? – user840175

+0

如果你想要它淡入淡出,你會將div的display屬性設置爲'none',或者你可以使用.fadeOut().load([ABOVE CODE])。fadeIn(); –

0

首先給你的鏈接一個id或class。我要叫它.loadpage

$('a.loadpage').click(function(e){ 
    e.preventDefault(); 
    var div = $('#replace'); 
    div.fadeOut(300).load($(this).attr('href'), function(){ div.fadeIn(300); }); 
}); 

首先,你需要做e.preventDefault()阻止鏈接的默認行爲。然後,您需要使用load()方法來引入您嘗試加載的頁面內容。

+0

淡出和淡出? – user840175

0

在Page1.html

$(function(){ 
    $("a").click(function(){ 
    $.get({url:"page2.html", 
      success: function(data){ 
      $("#replace").html($("#replace", $(data)).html()); 
    } 
    }); 
    }); 
});