Home Web Front-end JS Tutorial How to add child nodes in jquery

How to add child nodes in jquery

Nov 16, 2021 pm 02:49 PM
jquery

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>
Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

jQuery Tips: Quickly modify the text of all a tags on the page

In-depth analysis: jQuery's advantages and disadvantages In-depth analysis: jQuery's advantages and disadvantages Feb 27, 2024 pm 05:18 PM

In-depth analysis: jQuery's advantages and disadvantages

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Use jQuery to modify the text content of all a tags

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

Understand the role and application scenarios of eq in jQuery

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute?

See all articles