2013-05-10 129 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'); 
});