简体   繁体   English

如何使用jQuery附加元素,所有子元素以及父和子元素的所有类

[英]How to append an element, all its children, and all classes of the parent and children with jQuery

I have a function that is successful in removing an element and appending it elsewhere on the page as successful. 我有一个成功删除元素并将其附加到页面上其他位置的函数。 The problem is that as soon as the document is ready jQuery adds classes and attributes to the children that upon moving are lost. 问题在于,一旦文档准备就绪,jQuery就会向子级添加类和属性,而这些子类和属性在移动时会丢失。 I need these classes and attributes to remain after removing and appending. 在删除和附加后,我需要这些类和属性来保留。 I have thought about calling the original function that adds the classes, but the problem is they are key based and rely on their position prior to the move, calling it after changes the key and thus will add brand new and different classes. 我曾考虑过调用添加类的原始函数,但是问题是它们是基于键的,并且依赖于移动之前的位置,在更改键之后调用它,因此会添加全新的不同类。

The classes adding jQuery is pretty standard: 添加jQuery的类非常标准:

$(function(){
    $("div").each(function(key){
        if ($(this).hasClass("container")){
            $(this).find("ul").addClass("parent" + key);
            $(this).find(".container-item").attr("parentClass", ".parent" + key);
        };
    });
});

The remove/append function: 删除/追加功能:

function copy_item(draggable, target){
    var account = clone_items(draggable);
    //$('#'+name.uid).remove();
    $('#'+name.uid).hide();
    target.append(make_div(name, true, true));
    //$(draggable).children().attr("class", ($(draggable).children().attr("class")));
}
function make_div(name, drag, drop){
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', name.uid);
    newdiv.appendChild(make_h3(name.username));
    ul = document.createElement('ul');
    ul.setAttribute("class", "domain_list");
    newdiv.appendChild(ul);
    for (j = 0; j < name.domains.length; ++j) { 
        ul.appendChild(make_li(name.domains[j], drag));
    }
    return newdiv;
}

The end result in the HTMl is basically: HTM1的最终结果基本上是:

<div class="container">
    <ul class="parent0">
        <li parentClass="parent0">
        <li parentClass="parent0">

When recreating this structure, I need to have the class "parent0" and the parentClass attribute intact. 重新创建此结构时,我需要保持类“ parent0”和parentClass属性完整。 Above you can see I've tried hiding the element, ensuring that it still stays a valid element with the correct classes/attributes, but in the end that still didn't work out. 在上方可以看到我尝试隐藏该元素,以确保它仍然是具有正确类/属性的有效元素,但最后仍然无法解决。 Ideally, I could remove the element entirely and recreate it with the correct classes. 理想情况下,我可以完全删除该元素,然后使用正确的类重新创建它。

If I am correct in my understanding of what you are trying to do, you do not need to .remove() and recreate the element in order to move it. 如果我对您要做什么的理解是正确的,则无需移动.remove()并重新创建该元素即可移动它。 You can just do this: 您可以这样做:

function copy_item(draggable, target) {
    // not sure what this variable is for 
    // as you don't seem to be using it?
    var account = clone_items(draggable);

    // ...however, appending an existing 
    // element to another will 'move' it
    // and preserve all of it's properties
    target.append($('#' + name.uid));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM