Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中附加容器的内部内容而不丢失绑定_Javascript_Jquery_Html_Jquery Selectors_Append - Fatal编程技术网

Javascript 在jQuery中附加容器的内部内容而不丢失绑定

Javascript 在jQuery中附加容器的内部内容而不丢失绑定,javascript,jquery,html,jquery-selectors,append,Javascript,Jquery,Html,Jquery Selectors,Append,我试图在不丢失任何绑定的情况下将容器的内容从一个容器附加到另一个容器,我挠头想知道为什么这么难:D <div class="container"> <div class="field"> <label>Password</label> <input type="username" /> </div> </div> <div class="container

我试图在不丢失任何绑定的情况下将容器的内容从一个容器附加到另一个容器,我挠头想知道为什么这么难:D

<div class="container">
    <div class="field">
         <label>Password</label>
         <input type="username" />
    </div>
</div>
<div class="container">
    <div class="field">
         <label>Password</label>
         <input type="password" />
    </div>
</div>

// This puts the actual container in, I need the inner contents of it
$('.container').eq(0).append($('.container').eq(1));

// This loses any sort of binding that applies to what I'm moving
$('.container').eq(0).append($('.container').eq(1).html());

// This screws up the HTML
$('.container').eq(0).append($('*', $(container).eq(1)));

密码
密码
//这将实际的容器放入,我需要它的内部内容
$('.container').eq(0).append($('.container').eq(1));
//这会失去适用于我移动的任何类型的绑定
$('.container').eq(0).append($('.container').eq(1.html());
//这会破坏HTML
$('.container').eq(0).append($('*',$(container.eq(1));
看起来这是一项简单而普通的任务,但我不知道如何解决这个问题?我的第一个答案是将内容包装到另一个容器中,然后将其移动


你觉得怎么样?我是疯了还是这不可能D

这应该满足您的要求:

$('.container').eq(0).append($('.container').eq(1).children());

-您会注意到更改函数仍在附加字段上工作。

.html()
将元素序列化为字符串,这就是第二次尝试丢失事件绑定的原因。除了事件绑定之外,我不知道为什么第一个不起作用,如果第二个不起作用!是的,当然。D非常感谢。。。我不太清楚为什么我在8-)之前没有想到这一点