1

うまく説明できているかどうかを教えてください。.append()ページ内で div を繰り返すために使用しています。

HTML

 <html>
 <head>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
 <script>
  $(document).ready(function(){
    $("#num").click(function () {
      $('#form').append("<div class='Box'>I'm new box by prepend</div>");   
    });
  });
  </script>
  </head>

  <body>
    <div id="form">
       Hai Add Another by clicking the button
    </div>
    <button id="num">Click Me</button>
  /body>
 </html>

これは、div を繰り返し追加するときにうまく機能します。しかし、私は以下のコードのように外部から呼び出すのが好きです。

 <html>
  <head>
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
   <script>
    $(document).ready(function(){
      $("#num").click(function () {
        $('#form').append($('#Box'));   
      });
    });
  </script>
  </head>

  <body>
  <div id="form">
  Hai Add Another by clicking the button
  </div>
  <div class='Box'>I'm new box by prepend</div>
  <button id="num">Click Me</button>
 /body>
 </html>

なぜこれが機能しないのですか?

4

3 に答える 3

3

使ってみてくださいclone():

 <html>
 <head>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
 <script>
  $(document).ready(function(){
  $("#num").click(function () {
  $(".Box:first").clone().appendTo("#form"); 
});
});
  </script>
  </head>

  <body>
  <div id="form">
  Hai Add Another by clicking the button
  </div>
  <div class='Box'>I'm new box by prepend</div>
  <button id="num">Click Me</button>
 </body>
 </html>​

要素を追加する前に、新しい要素を作成する必要があります。

デモ

于 2012-12-01T05:10:22.680 に答える
3

これを試して

 $(document).ready(function(){
    $("#num").click(function () {
       $(".Box").first().clone().appendTo("#form"); 
    });
 });
于 2012-12-01T05:16:11.750 に答える
1

このようなことを試してください:-

 $("#parent_div").append('<div id="created_div"></div>');
于 2012-12-01T05:09:37.713 に答える