2009-08-21 33 views
3

我在我的文檔中使用了表格,我希望能夠讓用戶向列表中提交一個新項目,然後讓它「自動地」出現在列表的頂部(是的,這會更容易DIV,但與我的工作)。當使用jquery的clone()時,是否有跨瀏覽器的方式來忽略不透明?

我使用jQuery和clone()創建最近表格行的副本,然後使用fadeIn()在更新後顯示新項目並將其添加到列表頂部。因爲內部jQuery將元素(假設爲DIV)轉換爲'block',所以我也將css類更改爲'table-row'。它工作正常。

整個代碼是在這裏:

var row = $("tbody tr:first").clone().hide(); // clone and then set display:none 
    row.children("td[class=td-date]").html("today"); 
// set some properties 
    row.children("td[class=td-data]").html("data"); 
    row.children("td[class=td-type]").html("type"); 
// fadeIn new row at the top of the table. 
    row.insertBefore("tbody tr:first").stop().fadeIn(2000).css("display","table-row"); 

的問題是,如果我運行過程中速度過快 - 即淡入完成之前,「克隆()」命令結束了克隆的不透明度爲好。

其實我可以讓它在Firefox中使用通過調整上述第一線工作:

var row = $("tbody tr:first").clone().css("opacity","1").hide(); 

我現在擔心的是,我不知道任何的這種正在高效地完成,和/或「不透明」是跨瀏覽器安全依賴。

有沒有人做過這樣的事情,並可以提供更可靠的方法的任何指針?

回答

2

不透明度爲jQuery的CSS屬性是安全的跨瀏覽器,因爲它的鐵桿出在執行瀏覽器的差異。這裏的來源

// IE uses filters for opacity 
if (!jQuery.support.opacity && name == "opacity") { 
    if (set) { 
    // IE has trouble with opacity if it does not have layout 
    // Force it by setting the zoom level 
    elem.zoom = 1; 

    // Set the alpha filter to set the opacity 
    elem.filter = (elem.filter || "").replace(/alpha\([^)]*\)/, "") + 
    (parseInt(value) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); 
    } 

    return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? 
    (parseFloat(elem.filter.match(/opacity=([^)]*)/)[1])/100) + '': ""; 
} 

以下的工作。 Working Demo - 將/編輯添加到要與之一起播放的網址。

// stop previous animation on the previous inserted element 
    var prevRow = $("tbody tr:first").stop(true,true); 

    var row = prevRow.clone(); 
    row.children("td.td-date").text("today"); 
    row.children("td.td-data").text("data"); 
    row.children("td.td-type").text("type"); 

    row.fadeIn(2000).prependTo("tbody"); 
0

我認爲jQuery會處理它,如果你這樣做。

var row = $("tbody tr:first").clone().fadeIn(0).hide(); 
+0

在Firefox中不起作用,到目前爲止,css(「opacity」,1)似乎是最好的選擇,但尚未經過IE測試。 – FilmJ 2009-08-23 01:57:30

1

沒有理由在你的克隆上使用隱藏。該克隆沒有添加到dom中,因此它不可見。

試試這個:

var row = $("tbody tr:first").clone(); // clone 
// set some properties 
row.children("td[class=td-date]").html("today"); 
row.children("td[class=td-data]").html("data"); 
row.children("td[class=td-type]").html("type"); 
// fadeIn new row at the top of the table. 
row.insertBefore("tbody tr:first").fadeOut(0).fadeIn(2000).css("display","table-row"); 
相關問題