Home  >  Article  >  Web Front-end  >  How to add child nodes in jquery

How to add child nodes in jquery

青灯夜游
青灯夜游Original
2021-11-16 14:49:114621browse

Add method: 1. Use the "parent node.append(child node)" statement; 2. Use the "parent node.prepend(child node)" statement; 3. Use "child node.appendTo(parent node) )" statement; 4. Use the "$(child node).prependTo(parent node)" statement.

How to add child nodes in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, if you want to add child nodes to the parent node, there are many methods:

  • append(): to the "end" inside the selected element "Insert content.

  • appendTo(): ​​Insert content "at the end" inside the selected element.

  • prepend(): Insert content to the "beginning" inside the selected element.

  • prependTo(): ​​Insert content to the "beginning" inside the selected element.

Example:

  <script>
    $(function () {
      //1.append();
      //父节点.append(子节点); //作为最后一个子元素添加.
      $(&#39;#btnAppend&#39;).click(function () {
        //1.1 新创建一个li标签,追加到ul1中
        // var $newLi = $(&#39;<li>我是新创建的li标签</li>&#39;);
        // $(&#39;#ul1&#39;).append($newLi);

        //1.2 获取ul1中的某一个li标签,追加到ul1中.
        //剪切过来作为最后一个子元素添加.
        // var $li3 = $(&#39;#li3&#39;);
        // $(&#39;#ul1&#39;).append($li3);

        //1.3 获取ul2中的某一个li标签,追加到ul1中.
        //剪切过来作为最后一个子元素添加.
        var $li32 = $(&#39;#li32&#39;);
        $(&#39;#ul1&#39;).append($li32);
      });


      //2.prepend()
      //父节点.prepend(子节点); //作为第一个子元素添加.
      $(&#39;#btnPrepend&#39;).click(function () {
        //2.1 新创建一个li标签,追加到ul1中
        // var $newLi = $(&#39;<li>我是新创建的li标签</li>&#39;);
        // $(&#39;#ul1&#39;).prepend($newLi);

        //2.2 获取ul1中的某一个li标签,追加到ul1中.
        //剪切过来作为最后一个子元素添加.
        // var $li3 = $(&#39;#li3&#39;);
        // $(&#39;#ul1&#39;).prepend($li3);

        //2.3 获取ul2中的某一个li标签,追加到ul1中.
        //剪切过来作为最后一个子元素添加.
         var $li32 = $(&#39;#li32&#39;);
         $(&#39;#ul1&#39;).prepend($li32);
      });


      //3.appendTo();
      //子节点.appendTo(父节点); //作为最后一个子元素添加.
      $(&#39;#btnAppendTo&#39;).click(function () {
        //1.1 新创建一个li标签,追加到ul1中
        var $newLi = $(&#39;<li>我是新创建的li标签</li>&#39;);
        $newLi.appendTo($(&#39;#ul1&#39;));
      });
      
      //4.prependTo()
      //子节点.prependTo(父节点); //作为第一个子元素添加.
      $(&#39;#btnPrependTo&#39;).click(function () {
        //1.1 新创建一个li标签,追加到ul1中
        var $newLi = $(&#39;<li>我是新创建的li标签</li>&#39;);
        $newLi.prependTo($(&#39;#ul1&#39;));
      });


    });
  </script>
  <body>
<input type="button" value="append" id="btnAppend"/>
<input type="button" value="prepend" id="btnPrepend"/>
<input type="button" value="appendTo" id="btnAppendTo"/>
<input type="button" value="prependTo" id="btnPrependTo"/>
<ul id="ul1">
  <li>我是第1个li标签</li>
  <li>我是第2个li标签</li>
  <li id="li3">我是第3个li标签</li>
  <li>我是第4个li标签</li>
  <li>我是第5个li标签</li>
</ul>

<ul id="ul2">
  <li>我是第1个li标签2</li>
  <li>我是第2个li标签2</li>
  <li id="li32">我是第3个li标签2</li>
  <li>我是第4个li标签2</li>
  <li>我是第5个li标签2</li>
</ul>
</body>

How to add child nodes in jquery

Related tutorial recommendations: jQuery video tutorial

The above is the detailed content of How to add child nodes in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn