博主信息
博文 46
粉丝 3
评论 2
访问量 47080
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
jQuery文档操作(将节点插入、移动到目标节点) 2018年4月8日
墨雨的博客
原创
784人浏览过

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>

运行实例 »

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


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学