在两个容器之间移动div只能工作一次

时间:2013-09-08 10:23:47

标签: jquery

在我的代码中,当我点击div container_two时,我正试图将containerspan移到Add New。它只发生一次,但它第二次不会工作。由于需要对移动的<div>s进行更改,我需要使用两个单独的div。我想这与parent部分有关。你能看出这是如何起作用的吗?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Home</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $('.add').click(function() {
    var $move = $('#container_two .move');
    $(this).parent("div").after($move);
    $(".move").show();
    }); 
 });

</script>
</head>
<body>

<div id="container">
    <div class="h1" data-id="1">Heading One<span class="add" data-id="US01">Add New</span></div>
    <div class="h2" data-id="2">Sub One <span class="add" data-id="US02">Add New</span></div>
        <br>
    <div class="h1" data-id="8">Head Two <span class="add" data-id="US10">Add New</span></div>
    <div class="h2" data-id="9">Sub Two <span class="add" data-id="US20">Add New</span></div>
</div>

<div id="container_two">
    <div class="move" style="display:none">Mock Form</div>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果我找对你,你就错过了这个项目的克隆。

jsFiddle Demo

$('.add').click(function() {
    var $move = $('#container_two .move').clone();
    $(this).parent("div").after($move.show());
}); 
相关问题