如何正确克隆(jQuery)通过 PIE 应用样式的元素?

发布于 2024-10-27 08:46:00 字数 285 浏览 1 评论 0原文

我一直在一个新项目(专门针对IE8+),但是,在尝试克隆应用了 PIE 样式的元素时遇到了麻烦。

我在 此处 找到了一个 jsfiddle 来说明问题,欢迎输入(甚至是其他类似的 PIE 方法/替代方案) - 但是,.htc 文件不能跨域引用,因此这个小提琴仅包含我使用的实际标记和 CSS。

任何帮助表示赞赏。可能是什么原因造成的,是否有潜在的解决方法?

干杯, 人民

I have been using the .htc version of PIE successfully on a new project (that will specifically target IE8+), however, I'm having trouble when trying to clone an element that has PIE-style applied to it.

I got a jsfiddle illustrating the problem here, and input is welcome (even other, similar approaches/alternatives to PIE) - however, .htc files cannot be referenced cross-domain, so this fiddle just contains the actual markup and CSS I use.

Any help is appreciated. What could be causing this, is there a potential workaround?

Cheers,
peol

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

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

发布评论

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

评论(1

梨涡少年 2024-11-03 08:46:00

克隆具有 PIE 后代的元素时会遇到两个问题:

  1. PIE 插入的 VML 元素也将包含在克隆内容中,但由于某种原因,它们被错误地克隆,没有命名空间前缀,以及
  2. 唯一的 _pieId 属性PIE 在其每个目标元素上放置的内容也会复制到克隆中,这会导致不再唯一的 id 之间发生冲突。

因此,为了进行正确的克隆,我们需要摆脱两者。第一个可以通过暂时将每个 PIE 元素的行为样式属性设置为“无”来完成,然后克隆并恢复它。将其设置为“none”会触发 PIE 的清理方法,该方法会删除所有 VML 元素。第二项必须手动完成,因为 PIE 不会自动删除 _pieId 属性。这两个脚本都很容易编写。

下面是一个自定义 jQuery 扩展,它在我有限的测试中处理此问题:

jQuery.fn.cloneWithPIE = function(dataAndEvents, deepDataAndEvents) {
    // Find elements with PIE attached and remove their behaviors:
    var pied = this.find('[_pieId]').css('behavior', 'none');

    // Perform the clone:
    var clone = this.clone(dataAndEvents, deepDataAndEvents);

    // Remove the _pieId from each of the original and cloned 
    // elements, and restore the behavior:
    pied.add(clone.find('[_pieId]')).removeAttr('_pieId').css('behavior', '');

    return clone;
}

然后,您将调用 cloneWithPIE 方法,就像调用普通的克隆方法一样:

$('.someEl').cloneWithPIE().appendTo(newParent)

希望这对您有用。

There are two issues that are encountered when cloning elements with PIE'd descendants:

  1. The VML elements that PIE has inserted will also be included in the cloned content, but they are cloned incorrectly with no namespace prefix for some reason, and
  2. The unique _pieId attribute that PIE puts on each of its target elements is also copied in the clone, which leads to collisions between ids that are no longer unique.

So in order to do a proper clone, we need to get rid of both. The first can be done by temporarily setting the behavior style property of each PIE'd element to 'none' while cloning and restoring it afterward. Setting it to 'none' triggers PIE's cleanup methods which remove all the VML elements. The second item has to be done manually, as PIE does not remove the _pieId attributes automatically. Both of these are easy enough to script.

Here is a custom jQuery extension that handles this in my limited testing:

jQuery.fn.cloneWithPIE = function(dataAndEvents, deepDataAndEvents) {
    // Find elements with PIE attached and remove their behaviors:
    var pied = this.find('[_pieId]').css('behavior', 'none');

    // Perform the clone:
    var clone = this.clone(dataAndEvents, deepDataAndEvents);

    // Remove the _pieId from each of the original and cloned 
    // elements, and restore the behavior:
    pied.add(clone.find('[_pieId]')).removeAttr('_pieId').css('behavior', '');

    return clone;
}

You then would call the cloneWithPIE method just like you would call the normal clone method:

$('.someEl').cloneWithPIE().appendTo(newParent)

Hope that works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文