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

发布于 2024-08-02 12:04:53 字数 977 浏览 7 评论 0原文

我在文档中使用表格,并且希望能够让用户向列表提交新项目,然后让它“自动”出现在列表顶部(是的,使用 DIV 会更容易,但是与我所拥有的一起工作)。

我使用 jQuery 和 clone() 创建最新表行的副本,然后使用 fadeIn() 在更新和添加后显示新项目它到列表的顶部。因为 jQuery 在内部将元素(假设 DIV)转换为“块”,所以我还将 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();

我现在担心的是,我不确定这一切是否有效完成,和/或“不透明度”是否可以安全地跨浏览器依赖之上。

有没有人以前做过类似的事情,并且可以提供有关更可靠方法的任何指示?

I'm using tables in my document, and I want to be able to have a user submit a new item to a list, then have it "automagically" appear at the top of the list (yes, this would be easier with DIVs but working with what I have).

I'm using jQuery, and clone() to create a copy of the most recent table row, then using fadeIn() to display the new item after I update and add it to the top of the list. Because internally jQuery converts elements (assuming DIVs) to 'block', I'm also changing the css class to 'table-row'. It works fine.

The whole code is here:

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

The problem is that if I run the process too quickly - i.e. before the fadeIn completes, the "clone()" command ends up cloning the opacity as well.

I can actually get it to work in Firefox using by adjusting the first line above:

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

My concern now is that I'm not sure that any of this is being done efficiently, and/or that "opacity" is cross-browser safe to rely upon.

Has anyone done something like this before, and can offer any pointers on a more reliable approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪痕残 2024-08-09 12:04:53

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) + '': "";
}

这是以下作品的 。
工作演示 - 将 /edit 添加到 URL玩它。

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

opacity as a jQuery css attribute is safe cross-browser as it irons out the browser differences in the implementation. Here's the source

// 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) + '': "";
}

The following works.
Working Demo - add /edit to the URL to play with it.

  // 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");
北笙凉宸 2024-08-09 12:04:53

没有理由在克隆上使用 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");

There is no reason to use hide on your clone. The clone isn't added to the dom yet so it can't be visible.

Try this:

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");
冰雪梦之恋 2024-08-09 12:04:53

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

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

I think jQuery will handle it if you do this.

var row = $("tbody tr:first").clone().fadeIn(0).hide();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文