2009-08-02 34 views
0

我的網站因爲ajax調用而獲取值。之後,我想將結果(字符串)插入到標籤中。然而,我想插入結果的方式是(1)它必須有不透明度= 0,然後(2)它將slideDown(),所以整個內容列表使用動畫下推,最後(3)改變不透明度= 1.想象一下,這就像是一個Facebook消息列表插入過程使用jQuery在字符串中選擇標記

我打算這樣做的方式是首先從ajax返回結果字符串到opacity = 0。但是,我不知道如何使用jQuery從字符串中選擇標籤。我知道jQuery只能從DOM中選擇。那麼如何做到這一點?有什麼建議?

感謝

+0

你是什麼意思的'標記'? – 2009-08-02 19:33:14

+0

請發佈您的html片段 – pixeline 2009-08-02 19:33:59

+0

好吧,讓我們來做這個實驗吧。返回變量是str = string。假設價值回報低於 var str =「 aa bb」; 現在我想隱藏#aa,但它不起作用: $(str).find('#aa')。hide(); msgList.html(str + originalHtml); msgList中的結果仍顯示「aa」 – 2009-08-03 05:05:34

回答

0

隱藏()是一個jQuery的功能,這意味着只能一個jQuery對象,所以你必須先注入DOM中的HTML,然後調用隱藏()的DOM元素上。

$('#msgList').load (url,data,function(html){ 
var $this = $(this); // cache $(this) for performance 
$this.hide().html(html); // inject the html in the DOM 
$('#aa', $this).hide(); // hide #aa 
$this.show(); // reveal the messageList 
}); 
0

以下將使用簡單的.get()包裝器進行ajax調用。

然後回調函數會將響應加載到jquery對象中並找到您想要的標記。

然後隱藏該標籤並將其附加到div容器。

最後它會滑下來。

//do ajax call 
$.get(url, function(html) { 

    //load html into a jQuery object, find the tag by id then hide it 
    var $el = $(html).filter('#someId').hide(); 
    //append element to the dom and slide it down 
    $el.appendTo('#someDiv').slideDown('slow'); 

}); 
0

我會考慮將返回值放在SPAN中。隱藏將容納它的容器。將SPAN的不透明度設置爲0,然後將結果添加到容器並將其放入容器中。一旦完成,向下滑動容器並顯示slideDown方法回調結果的動畫。

$.ajax({ 
    ... 
    success: function(data) { 
     var container = $('#container').hide(); 
     $('<span />').css('opacity', 0) 
         .html(data.result) 
         .appendTo(container); 
     container.slideDown('normal', function() { 
         $(this).('span:first') 
           .animate({ 'opacity': 1.0 }); 
        }); 
    } 
    ... 
});