使用jquery的clone()时是否有跨浏览器的方法来忽略不透明度?
我在文档中使用表格,并且希望能够让用户向列表提交新项目,然后让它“自动”出现在列表顶部(是的,使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
opacity 作为 jQuery css 属性是安全的跨浏览器,因为它消除了实现中的浏览器差异。 来源
这是以下作品的 。
工作演示 - 将 /edit 添加到 URL玩它。
opacity as a jQuery css attribute is safe cross-browser as it irons out the browser differences in the implementation. Here's the source
The following works.
Working Demo - add /edit to the URL to play with it.
没有理由在克隆上使用 hide。克隆尚未添加到 dom,因此它不可见。
试试这个:
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:
我认为如果你这样做,jQuery 会处理它。
I think jQuery will handle it if you do this.