QUIZGUM

Coding Class

Quizgum : append

Move tag

append();

I will explain it in source because it is too hard for me to explain.

  1. <div class="ex_wrap" style="width:200px;height:100px;border:1px solid #000"></div>
  2. <p>asimo</p>
  3. <p>honda</p>
  4. <p>iphone5</p>
  5. <p>macbook air</p>

You can use jQuery to change the source above to: The source of the actual editor is not like this, but you can make it like this.

  1. <div class="ex_wrap" style="width:200px;height:100px;border:1px solid #000">
  2. <p class="ex_p">asimo</p>
  3. <p class="ex_p">honda</p>
  4. <p class="ex_p">iphone5</p>
  5. <p class="ex_p">macbook air</p>
  6. </div>

The first source is div tag and p tag on the same line.
The second source contains the p tags inside the div tag.
It is append() that functions to put any tag into any tag. ^^

How To use append()

  1. Target of eating.append(Target to be eaten);

That is what happened.

  1. $('.ex_wrap').append('.ex_p');

Then, you may not know this well, so let's check it through the example to make it easier to understand.

CSS

  1. .ex_p{border:5px solid yellow}
  2. .ex_wrap p{clear:both;float:left;margin-left:20px;border:0}

asimo

honda

iphone5

macbook air

In the example above, the ex_p tags are not in ex_wrap yet.

Let's take a look at the following jQuery.

jQuery

  1. $(function(){
  2. var ex_wrap = $('.ex_wrap');
  3. var ex_p = $('.ex_p');
  4. ex_wrap.append(ex_p);
  5. });

asimo

honda

iphone5

macbook air

As above, asimo honda iphone5 macbook air enters ex_wrap.

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>quizgum :: jQuery Course</title>
  6. <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js" ></script>
  7. <script type="text/javascript">
  8. $(function(){
  9. var ex_wrap = $('.ex_wrap');
  10. var ex_p = $('.ex_p');
  11. ex_wrap.append(ex_p);
  12. });
  13. </script>
  14. <style>
  15. .ex_wrap{float:left;border:1px solid #000000}
  16. .ex_wrap p{clear:both;float:left;margin-left:20px;border:0}
  17. </style>
  18. </head>
  19. <body>
  20. <div class="ex_wrap"></div>
  21. <p class="ex_p">asimo</p>
  22. <p class="ex_p">honda</p>
  23. <p class="ex_p">iphone5</p>
  24. <p class="ex_p">macbook air</p>
  25. </body>
  26. </html>

Now let's look at appendTo().

You can think of the reverse order.

  1. Target to be eaten.appendTo(Target of eating);

If you edit the source above

  1. $(function(){
  2. var ex_wrap = $('.ex_wrap');
  3. var ex_p = $('.ex_p');
  4. ex_p.appendTo(ex_wrap);
  5. });
  6.  
  7. In other words, ex_p works to ex_wrap

Then practice with an example.

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>quizgum :: jQuery Course</title>
  6. <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js" ></script>
  7. <script type="text/javascript">
  8. $(function(){
  9. var ex_wrap = $('.ex_wrap');
  10. var ex_p = $('.ex_p');
  11. ex_p.appendTo(ex_wrap);
  12. });
  13. </script>
  14. <style>
  15. .ex_wrap{float:left;border:1px solid #000000}
  16. .ex_wrap p{clear:both;float:left;margin-left:20px;border:0}
  17. </style>
  18. </head>
  19. <body>
  20. <div class="ex_wrap"></div>
  21. <p class="ex_p">asimo</p>
  22. <p class="ex_p">honda</p>
  23. <p class="ex_p">iphone5</p>
  24. <p class="ex_p">macbook air</p>
  25. </body>
  26. </html>

This concludes the tutorial on append() and appendTo(). ^^

The next lesson will be about val(), empty(), and remove().