最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding a div dynamically using append() - Stack Overflow

programmeradmin0浏览0评论

Kindly notify me I am explaining it well or not. I'm using .append() to repeat a div inside a page.

HTML

 <html>
 <head>
 <script type="text/javascript" src=".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>

This works good when I add a div repeatedly. But I like to call it externally like the below code.

 <html>
  <head>
   <script type="text/javascript" src=".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>

Why is this not working?

Kindly notify me I am explaining it well or not. I'm using .append() to repeat a div inside a page.

HTML

 <html>
 <head>
 <script type="text/javascript" src="https://ajax.googleapis./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>

This works good when I add a div repeatedly. But I like to call it externally like the below code.

 <html>
  <head>
   <script type="text/javascript" src="https://ajax.googleapis./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>

Why is this not working?

Share Improve this question edited Dec 1, 2012 at 5:14 Mr_Green 41.9k47 gold badges170 silver badges276 bronze badges asked Dec 1, 2012 at 5:06 Vivek DragonVivek Dragon 2,2485 gold badges28 silver badges48 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try using clone():

 <html>
 <head>
 <script type="text/javascript" src="https://ajax.googleapis./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>​

You have to create a new element before adding it.

Demo

try this

 $(document).ready(function(){
    $("#num").click(function () {
       $(".Box").first().clone().appendTo("#form"); 
    });
 });

Try something like this:-

 $("#parent_div").append('<div id="created_div"></div>');
发布评论

评论列表(0)

  1. 暂无评论