2012-04-16 65 views
0

我想淡化它的文本更改div,我做了一個小函數,但它不工作。它改變價值,但不會消失。在jQuery中淡入或淡出

$(function(){ 
    $('.jqTransformSelectWrapper').find('ul').find('a').click(function(){ 
     var cityVal= $('.jqTransformSelectWrapper').find('ul').find('.selected').text(); 
     var capCity= cityVal.toLowerCase(); 

     if(capCity == "first"){ 
      $('#flightPrice').text('1').fadeOut('slow');    
     } 
    }) 
}) 

// HTML

<a index="1" href="#" class="selected">first</a> 

<span id="flightPrice">val</span> 
+2

可能還張貼您的標記嗎? – 2012-04-16 09:50:24

+0

'var capCity = cityVal.toLowerCase();'你知道那些心理學實驗,他們給你提供了相互衝突的刺激來混淆你嗎?那條線就是這樣。 ;-) – 2012-04-16 09:52:44

回答

1

你想要包裝的跨度的文本,然後淡出了這一點:

<div class="button"><span>TEXT!</span></div> 

,你不希望使用fadeOut,因爲這將改變你的按鈕的大小,因爲文字會消失,一旦fadeOut結束,不再佔用空間。取而代之的動畫不透明度:

$(".button").click(function(){ 
    $(this).find("span").animate({opacity:0},function(){ 
     $(this).text("new text") 
      .animate({opacity:1}); 
    }) 
}); 

http://jsfiddle.net/8Dtr6/

編輯:小幅回調,只要你馬上漸退它似乎並沒有被使用fadeInfadeOut,至少在瀏覽器的問題。我希望可能在較小的瀏覽器中看到輕微的閃爍,但可能是錯誤的。

可能使用隊列,避免回調位清潔:

$(".button").click(function(){ 
    $(this).find("span") 
     .animate({opacity:0}) 
     .queue(function(){ 
      $(this).text("new text"); 
      $(this).dequeue() 
     }) 
     .animate({opacity:1}); 
}); 

http://jsfiddle.net/8Dtr6/2