2010-05-25 121 views
2

我試圖克隆樹,從中刪除一個元素,然後將結果附加到新的地方。但問題是該元素沒有被刪除,它總是追加原始樹。jquery - 從克隆節點中刪除特定元素

$(".js-parent-trigger").click(function() {   
    var commentForm = $("#js-add-comment-wrapper").clone(true).css("margin", "10px").remove(".js-add-comment-title"); 
    $(this).parents(".js-comment-wrapper").append(commentForm); 
    return false; 
}); 
+1

@aSeptik - 您可以使用工作的元素追加它們之前。 – user113716 2010-05-25 15:26:52

+0

帕特里克,你的回答是正確的,你爲什麼刪除它? – user313382 2010-05-25 15:27:32

+0

我被刪除是因爲我認爲我犯了一個錯誤。我沒有意識到你可以在選擇器中使用'.remove()',所以我正在做一些研究,以查看返回的元素。現在回到網上。 :) – user113716 2010-05-25 15:29:50

回答

3
$(".js-parent-trigger").click(function() {   
    var commentForm = $("#js-add-comment-wrapper").clone(true).css("margin", "10px"); 
    commentForm.find(".js-add-comment-title").remove(); 
    $(this).parents(".js-comment-wrapper").append(commentForm); 
    return false; 
}); 
2

忍不住使得一個大的聲明:

$(".js-parent-trigger").click(function() {   
    $("#js-add-comment-wrapper").clone(true).css("margin", "10px") 
     .find(".js-add-comment-title").remove() 
     .end().appendTo('.js-commet-wrapper'); 
    return false; 
}); 
+0

+1爲了純粹的美感! :) – user113716 2010-05-25 16:18:47