2012-10-04 165 views
0

我想淡出div元素,改變它裏面的文本,並在褪色回來......jQuery的淡入淡出和

我的代碼是在這裏 -

<script> 
     $(document).ready(function(){ 
      $("#l2").click(function(event){ 
       event.preventDefault(); 
       $("#1").fadeOut(); 
       $("#1").replaceWith('Testing'); 
       $("#1").fadeIn(); 
      }); 
     }); 
    </script> 

l2被點擊,它將fadeOut div id 1替換並淡入......但它失敗了。ANY謝謝!

+0

數字ID不建議... – henryaaron

+2

的'div' ID必須啓動用字母 – Darcy

+0

什麼是'測試'?你想要取代什麼? –

回答

3

你想用.text之前沒有看到它或.html而不是.replaceWith - 這是爲了使用DOM元素。此外,.text.html不適用於隊列,因此您無法鏈接它們。你必須使用的回調:

$("#1").fadeOut(function() { 
    $(this).text('Testing').fadeIn(); 
}); 
+0

感謝這工作! – Matt

0

只需使用淡出回調。這裏是一個快速小提琴http://jsfiddle.net/M5PnY/2/單擊文本

編輯------

改變與HTML()代替,

$(document).ready(function(){ 
    $("#1").click(function(event){ 
     event.preventDefault(); 
     $("#1").fadeOut(function(event){ 
      $("#1").html('Testing').fadeIn(); 
     }); 
    }); 
});​ 
+0

因爲'.replaceWith'刪除了ID,所以不起作用。請注意,它不會褪色。 –

+0

你是對的沒有看到,用html()代替 – user1289347

1

您需要使用回調來實現自己的目標:

$("#1").fadeOut(500, function(){ 
    $("#1").html('Testing'); 
    $("#1").fadeIn(); 
}); 

的淡出完成時作爲參數傳遞的功能將被執行。

0

在第二行中,您將元素#1替換爲新的元素,然後您不能再次將其淡入。

$("#1").replaceWith('Testing'); 

導致DIV#1由文本測試替代

,所以你應該使用:

$("#1").html('Testing');