【问题标题】:Jquery Clone() modify clone after or before insertJquery Clone() 在插入之后或之前修改克隆
【发布时间】:2013-09-23 18:25:37
【问题描述】:

环境

我想要什么

复制该框,但从复制的子项中删除“hure”类

要克隆的盒子的结构

<div name="box">
    <a .../>
    <span..../>
    <span..../>
    <div class="eye"> <img class="hure"..../></div>
...
</div>

2考虑

  • 我使用通用的拖拽和克隆功能,所以只有10%的情况下会有img标签并且可以找到类“hure”,其他的还有其他输入字段。
  • 克隆的元素位于原始元素之后的 dom 结构中(可能对搜索很重要)。

我的方法:

导致两个类都被删除,为什么不只是克隆?

helper : function(ev,el) { 
        if ($(this).find(".hure").length > 0){
        return($(el).find(".hure").removeClass("hure").clone());}
        else 
        {
        return($(this).clone());}
        },

感谢您的帮助,自从尝试做那件事几个小时以来,我就迷路了。我找不到将原始版本与 ***g 克隆版本区分开来的代码。

编辑

另一种方法是在克隆之后这样做,我已经尝试过但没有成功。

我的失败方法

if ($(this).find(".hure").length > 0){ $(this).find(".hure").eq(1).removeClass("hure");}

解决方案

stop: function() {
        if ($(".hure").length>0){
        $(".hure:eq(1)").removeClass("hure");}
        }

【问题讨论】:

  • 我在使用 jQuery 中的帮助回调时遇到了类似的挫折,并且无法确定如何正确持久化克隆。我认为可能需要调整 jQuery 的源代码以适应您的特定要求。
  • 您是否尝试过在克隆元素后删除类??
  • @travis 如果我理解正确的话听起来很糟糕 - 所以没有 str8 方法可以简单地做到这一点,即使是 4you ;)?
  • @Jason P +1 这是我第一次尝试做“之后”但没有成功,感谢任何帮助-C 我的编辑。

标签: jquery jquery-selectors clone


【解决方案1】:

jsFiddle Demo

我想出了这个解决方法。基本上,一旦拖动运动停止,您就可以绑定一个事件。当您在停止事件中克隆元素时,您可以随意放置它。此时您应该操作克隆。在演示中,它的类“hure”被移除,并被放置在主体中。可以在此处确定放置在其他位置。

$('.box').draggable({
 helper: 'clone',
 stop: function(ev,ui){
  //clone helper
  var copy = $(this).clone();
  //use exposed api to set position
  copy.position(ui.position);
  copy.offset(ui.offset);
  //search for the class in copy and remove it
  $(".hure",copy).removeClass("hure");
  //place element (this may affect position/offset)
  document.body.appendChild(copy[0]);
 }
});

【讨论】:

  • +1 感谢工作代码。非常独特的想法,很酷。在我的特殊情况下,我使用在我的 Q 中编辑的解决方案。如果您认为我的方法可能很麻烦,请分享。谢谢 4 的帮助答案。
【解决方案2】:

试试这个..先克隆,然后删除克隆上的类:

function (ev, el) {
    var $cloned = $(this).clone();
    $cloned.find(".hure").removeClass("hure");
    return $cloned;
}

【讨论】:

  • 谢谢。这让我发疯,这不起作用,没有机会将克隆与原版区分开来。
猜你喜欢
  • 2011-11-14
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多