gpt4 book ai didi

Jquery Clone() 在插入之后或之前修改克隆

转载 作者:行者123 更新时间:2023-12-01 01:15:25 24 4
gpt4 key购买 nike

环境

我想要什么

复制该框,但从复制的子项中删除“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");}
}

最佳答案

jsFiddle Demo

我想出了这个解决方法。基本上,一旦拖动运动停止,您就可以绑定(bind)一个事件。当您在停止事件中克隆元素时,您可以根据需要放置它。此时您应该操作克隆。在演示中,它的类“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]);
}
});

关于Jquery Clone() 在插入之后或之前修改克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18966205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com