使用jquery的clone()时是否有跨浏览器忽略不透明度的方法?

时间:2009-08-21 23:43:55

标签: javascript jquery clone effects

我在我的文档中使用表格,我希望能够让用户将新项目提交到列表中,然后将其“自动”显示在列表的顶部(是的,这会更容易和DIV一起工作但是和我一起工作)。

我正在使用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"); 

问题在于,如果我过快地运行该过程 - 即在fadeIn完成之前,“clone()”命令最终也会克隆不透明度。

我可以通过调整上面的第一行来实际使用它在Firefox中工作:

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

我现在关注的是,我不确定这些是否有效完成,和/或“不透明度”是跨浏览器安全依赖的。

之前有没有人做过这样的事情,并且可以提供更可靠方法的指示?

3 个答案:

答案 0 :(得分:2)

opacity作为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 - 将 / edit 添加到网址以进行播放。

  // 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");

答案 1 :(得分:1)

没有理由在克隆上使用hide。克隆未添加到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");

答案 2 :(得分:0)

我认为如果你这样做,jQuery会处理它。

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