2013-09-23 156 views
3

環境或插入之前Jquery的克隆()修改後克隆

我想

複製這個箱子是什麼,但刪除類 「呼熱」來自複製的孩子

待克隆盒的結構

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

2Consider

  • 我使用一般的功能拖拽和克隆,所以只在的情況下,10%會出現在其中一個img標籤和類「呼熱」,可以發現,其他人則有其他輸入字段。
  • 克隆的元素在dom結構之後原始(可能對搜索很重要)。

我的方法:

得到的,這兩個類得到去除,whytf不僅克隆?

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

感謝您的幫助,我迷失了幾小時試圖做的事情。我找不到代碼來區分***克隆中的原始代碼。

編輯

的另一種方法是做到這一點的克隆後,我已經嘗試過,但沒有成功。

我successless方法

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

解決方案

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

我在jQuery中使用helper回調時遇到了類似的沮喪,無法確定如何正確保存克隆。我認爲可能需要調整jQuery的源代碼以適應您的特定需求。 –

+1

您是否在克隆元素後嘗試刪除類**? –

+0

@travis聽起來不太好,如果我理解正確 - 所以沒有str8方法以簡單的方式做到這一點,甚至4you;)? – Email

回答

2

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]); 
} 
}); 
+0

+1 thx for working code。與位置非常獨特的想法,很酷。在我個人的情況下,我使用了像我Q中編輯的解決方案,如果您認爲我的方法可能會很麻煩,請分享。 thx 4幫助答案。 – Email

0

試試這個..克隆,然後再刪除類的克隆:

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

thx。它讓我瘋狂,這不起作用,沒有機會將克隆從原始的不同。 – Email