开发者

Appending inner contents of a container without losing binding in jQuery

开发者 https://www.devze.com 2023-04-06 21:16 出处:网络
I\'m trying to append the contents of a container from one to the other without losing any sort of binding and I\'m scratching my head wondering why it\'s so difficult :D

I'm trying to append the contents of a container from one to the other without losing any sort of binding and I'm scratching my head wondering why it's so difficult :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(开发者_JAVA技巧));

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

Seems like such a simple and common task but I've got no idea how to get around this? My first answer would be to wrap the content in another container and move that instead.

What d'ya think? Am I going mad or is this impossible? :D


This should do what you want:

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

JSBin Example - You'll notice the change function still works on the appended field.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号