Blogger Information
Blog 46
fans 3
comment 2
visits 39294
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jQuery文档操作(将节点插入、移动到目标节点) 2018年4月8日
墨雨的博客
Original
611 people have browsed it

appendTo(),prependTo(),insertAfter(),insetBefore()方法演示代码:


实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>jQuery文档操作(将节点添加或移动到目标节点)</title>
	<style type="text/css">
		table{
            border-collapse: collapse;
			width: 800px;
			height: 300px;
			margin: 20px auto;
		}
		table,td,th{	
			border: 1px solid #555;
		}

		li {
			list-style: none;
			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>

	<p style="background-color: orange;width: 300px;">我是要被appendTo()移动的节点1</li>
	<p style="background-color: orange;width: 300px;">我是要被prependTo()移动的节点2</li>
	<p style="background-color: orange;width: 300px;">我是要被insertAfter()移动的节点3</li>
	<p style="background-color: orange;width: 300px;">我是要被insertBefore()()移动的节点4</li>
	
	<hr>

<table>
	<caption>jQuery文档操作</caption>
	<tr>
		<td>方法</td>
		<td>appendTo()</td>
		<td>prependTo()</td>
		<td>insertAfter()</td>
		<td>insertBefore()</td>
	</tr>
	<tr>
		<td>语法</td>
		<td>content.appendTo(target)</td>
		<td>content.prepend(target)</td>
		<td>content.insertAfter(target)</td>
		<td>content.insertBefore(target)</td>
	</tr>
	<tr>
		<td>参数</td>
		<td>要添加或移动到的节点</td>
		<td>要添加或移动到的节点</td>
		<td>要插入的节点</td>
		<td>要插入的节点</td>
	</tr>

	<tr>
		<td>功能</td>
		<td>插入到目标元素内容的后面</td>
		<td>插入到目标元素内容的前面</td>
		<td>插入到目标节点的后面</td>
		<td>插入到目标元素的前面</td>
	</tr>
	<tr style="text-align: center;">
 		<td>演示</td>
        <td><button>演示</button></td>
        <td><button>演示</button></td>
        <td><button>演示</button></td>
        <td><button>演示</button></td>
	</tr>

</table>

</body>
</html>
<script type="text/javascript" src="../jquery-3.3.1.js"></script>
<script type="text/javascript">
	$('button').eq(0).on('click',function(){
		var li = $('<li>').css('background-color','lightgreen').html('我是appendTo()新生成的节点1')
		li.appendTo($('ul'))
	})
	$('button').eq(1).on('click',function(){
		$('p:eq(1)').prependTo($('ul'))

	})

	$('button').eq(2).on('click',function(){
		var p = $('<li>').css('background-color','lightgreen').html('我是insertAfter()新生成的节点3')
		p.insertAfter($('ul'))
	})
	$('button').eq(3).on('click',function(){
		var p = $('<li>').css('background-color','lightgreen').html('我是insertBefore()新生成的节点4')
		$('p:eq(3)').insertBefore($('li:eq(2)'))
	})
	
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post