Home >Web Front-end >JS Tutorial >jquery add or move node on target node

jquery add or move node on target node

无忌哥哥
无忌哥哥Original
2018-06-29 14:26:101362browse

jquery adds or moves nodes on the target node

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>在目标节点上添加或移动节点</title>
<style type="text/css">
li {
background-color: lightskyblue;
width: 300px;
margin-bottom: 5px;
}
</style>
</head>
<body>
<ul>
<li>列表项1</li>
<li>列表项2</li>
<li>列表项3</li>
<li>列表项4</li>
<li>列表项5</li>
</ul>
<button>append()</button>
<button>prepend()</button>
<button>after()</button>
<button>before()</button>
<p style="background-color: orange;width: 300px;">我是要被append()移动的节点1</li>
<p style="background-color: orange;width: 300px;">我是要被prepend()移动的节点2</li>
<p style="background-color: orange;width: 300px;">我是要被after()移动的节点3</li>
<p style="background-color: orange;width: 300px;">我是要被before()移动的节点4</li>
</body>
</html>

* 1. Insertion position:

* 1.1: Before and after the node content

* 1.2: Before and after the node

* 2. Node to be inserted:

* 2.1: For newly created nodes: call the add operation

* 2.2: For existing nodes: call the move operation

* 3. So there should be four corresponding methods

* 3.1: After inserting into the node content: append()

* 3.2: Before inserting into the node content: prepend()

* 3.3: After inserting into the node: after()

* 3.3: Before inserting into the node: before() *

* 1.append()

* Syntax: target.append(content)

* Parameter: Node to be added or moved

* Function: Insert after the content of the target element

$(&#39;button&#39;).eq(0).on(&#39;click&#39;,function(){
//1. 添加操作
//第一步: 生成节点元素,添加内容,并设置样式
var li = $(&#39;<li>&#39;).css(&#39;background-color&#39;,&#39;lightgreen&#39;).html(&#39;我是append()新生成的节点1&#39;)
//第二点: 将新节点插入到目标节点内容的后面
$(&#39;ul&#39;).append(li)
//2.移动操作(请将添加操作的代码注释掉)
// $(&#39;ul&#39;).append($(&#39;p:first&#39;))
})

* 2.prepend()

* Syntax: target.prepend(content)

* Parameters: Node to be added or moved

* Function: Insert into In front of the content of the target element

$(&#39;button&#39;).eq(1).on(&#39;click&#39;,function(){
//1. 添加操作
//第一步: 生成节点元素,添加内容,并设置样式
var li = $(&#39;<li>&#39;).css(&#39;background-color&#39;,&#39;lightgreen&#39;).html(&#39;我是prepend()新生成的节点2&#39;)
//第二点: 将新节点插入到目标节点内容的后面
$(&#39;ul&#39;).prepend(li)
//2.移动操作(请将添加操作的代码注释掉)
// $(&#39;ul&#39;).prepend($(&#39;p:eq(1)&#39;))
})

* 3.after()

* Syntax: target.after(content)

* Parameters: The node to be inserted

* Function: Insert behind the target node

$(&#39;button&#39;).eq(2).on(&#39;click&#39;,function(){
//1. 添加操作
//第一步: 生成节点元素,添加内容,并设置样式
var p = $(&#39;<li>&#39;).css(&#39;background-color&#39;,&#39;lightgreen&#39;).html(&#39;我是after()新生成的节点3&#39;)
//第二点: 将新节点插入到目标节点的后面
$(&#39;ul&#39;).after(p)
// $(&#39;li:eq(1)&#39;).after(p)
//2.移动操作(请将添加操作的代码注释掉)
// $(&#39;ul&#39;).after($(&#39;p:eq(2)&#39;))
// $(&#39;li:eq(1)&#39;).after($(&#39;p:eq(2)&#39;))
})

* 4.before()

* Syntax: target.after(content)

* Parameters: To insert Node

* Function: Insert in front of the target element

$(&#39;button&#39;).eq(3).on(&#39;click&#39;,function(){
//1. 添加操作
//第一步: 生成节点元素,添加内容,并设置样式
var p = $(&#39;<li>&#39;).css(&#39;background-color&#39;,&#39;lightgreen&#39;).html(&#39;我是before()新生成的节点4&#39;)
//第二点: 将新节点插入到目标节点的后面
$(&#39;ul&#39;).before(p)
// $(&#39;li:eq(2)&#39;).before(p)
//2.移动操作(请将添加操作的代码注释掉)
// $(&#39;ul&#39;).before($(&#39;p:eq(3)&#39;))
// $(&#39;li:eq(2)&#39;).before($(&#39;p:eq(3)&#39;))
})

The above is the detailed content of jquery add or move node on target node. 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
Previous article:An introduction to vuexNext article:An introduction to vuex