2013-05-10 73 views
0

我有一個jQuery fadeIn()函數的小問題。基本上,我擁有的是作爲主顯示屏幕的一部分。我想淡入divs進出這個區域。目前,我已經爲淡出從那裏開始的div工作,但是當我嘗試淡入其他div時,沒有任何反應。這是我迄今爲止的代碼。jQuery fadeIn()與fadeOut()不兼容

$('#aboutbtn').click(function(e){  
    $('#slideshowContainer').fadeOut('fast', function(){ 
     $('#slideshowContainer').replace('<div id="about"></div>').fadeIn('slow'); 
     }); 

就像我說的那樣,這會淡出slideshowContainer div,但關於div並沒有進入它的位置。

更新 -

好了,這是令人尷尬的,哈哈。我試圖引用一個我已經在我的HTML中使用的div,所以我認爲replaceWith('')是沒有意義的。

如果我想用我已經在我的HTML文檔中定義的div替換,應該不是這樣嗎?

$('#aboutbtn').click(function(e){  
$('#slideshowContainer').fadeOut('fast', function(){ 
    $('#slideshowContainer').replace('#about').fadeIn('slow'); 
    }); 

我想要替換淡出div的div的id是。但是,當我這樣做時,它只顯示#about。

+1

這是什麼'.replace('

')'應該做的? – j08691 2013-05-10 02:49:23

+0

他可能是指replaceWith()? – Edper 2013-05-10 02:52:44

+0

我的意圖是將它放在已淡出的div所在的位置。這是不正確的嗎? FWIW,我用replaceWith()也試過了,但還是沒有運氣。 – user2363182 2013-05-10 02:55:59

回答

0

jQuery對象沒有replace方法,使用replaceWith

$('#slideshowContainer').fadeOut('fast', function() { 
    $(this).replaceWith(function() { 
     return $('<div id="about">about</div>').hide().fadeIn(); 
    }); 
}); 

http://jsfiddle.net/ypgwL/

更新:

$('#slideshowContainer').fadeOut('fast', function() { 
    var $d = $('#about'); 
    $(this).replaceWith($d); 
    $d.fadeIn(); 
}); 

http://jsfiddle.net/ujWQW/

2

儘量把文本UR d內四

here's my jsfiddle

$(document).ready(function() { 
$('#aboutbtn').click(function (e) { 
    $('#slideshowContainer').fadeOut('fast', function() { 
     $('#slideshowContainer').replaceWith('<div id="about">You miss this thing!  </div>').fadeIn('slow'); 
    }); 
}); 
}); 
0

這裏是什麼,我認爲你正在做一個完整的例子:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#aboutbtn').click(function(e){  
     $('#slideshowContainer').fadeOut('fast', function(){ 
      ele = $('<div id="about" style="display:none"></div>').fadeIn('slow'); 
      $('#slideshowContainer').replaceWith(ele) 
     }); 
    }); 
}); 
</script> 

<div id="slideshowContainer"></div> 

<input type="button" name="button" value="Button" id="aboutbtn"> 
+0

這正是我想要做的和有意義的,謝謝。不過,我在原始帖子中增加了一些內容。如果我已經在HTML中定義了div並且不需要在函數中聲明它,那麼該怎麼辦? – user2363182 2013-05-10 03:08:18

0

試試這個

$('#aboutbtn').click(function(e){  
$('#slideshowContainer').fadeOut('fast', function(){ 
$(this).replaceWith('#about').fadeIn('slow'); 
}); 

或者你可以使用AppendTo代替rplacewith

$('#aboutbtn').click(function(e){  
$('#slideshowContainer').fadeOut('fast', function(){ 
$(this).appendTo('#about').fadeIn('slow'); 
});