How to add child nodes in jquery
Nov 16, 2021 pm 02:49 PMAdd 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.
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(子节点); //作为最后一个子元素添加. $('#btnAppend').click(function () { //1.1 新创建一个li标签,追加到ul1中 // var $newLi = $('<li>我是新创建的li标签</li>'); // $('#ul1').append($newLi); //1.2 获取ul1中的某一个li标签,追加到ul1中. //剪切过来作为最后一个子元素添加. // var $li3 = $('#li3'); // $('#ul1').append($li3); //1.3 获取ul2中的某一个li标签,追加到ul1中. //剪切过来作为最后一个子元素添加. var $li32 = $('#li32'); $('#ul1').append($li32); }); //2.prepend() //父节点.prepend(子节点); //作为第一个子元素添加. $('#btnPrepend').click(function () { //2.1 新创建一个li标签,追加到ul1中 // var $newLi = $('<li>我是新创建的li标签</li>'); // $('#ul1').prepend($newLi); //2.2 获取ul1中的某一个li标签,追加到ul1中. //剪切过来作为最后一个子元素添加. // var $li3 = $('#li3'); // $('#ul1').prepend($li3); //2.3 获取ul2中的某一个li标签,追加到ul1中. //剪切过来作为最后一个子元素添加. var $li32 = $('#li32'); $('#ul1').prepend($li32); }); //3.appendTo(); //子节点.appendTo(父节点); //作为最后一个子元素添加. $('#btnAppendTo').click(function () { //1.1 新创建一个li标签,追加到ul1中 var $newLi = $('<li>我是新创建的li标签</li>'); $newLi.appendTo($('#ul1')); }); //4.prependTo() //子节点.prependTo(父节点); //作为第一个子元素添加. $('#btnPrependTo').click(function () { //1.1 新创建一个li标签,追加到ul1中 var $newLi = $('<li>我是新创建的li标签</li>'); $newLi.prependTo($('#ul1')); }); }); </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>
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery?

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

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

In-depth analysis: jQuery's advantages and disadvantages

Use jQuery to modify the text content of all a tags

Understand the role and application scenarios of eq in jQuery

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