博主信息
博文 51
粉丝 3
评论 1
访问量 44163
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
JQuery appendTo() prependTo() insertAfter() insertBefore()—2018年4月9日10时26分
Gee的博客
原创
680人浏览过

appendTo() prependTo() insertAfter() insertBefore()

将节点添加或移动到目标节点的操作

汉诺塔小动画:

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>homeword</title>
	<style type="text/css">
		.box {
			width: 600px;
			height: 300px;
			border: 1px solid #888;
		}

		.line1 {
			width: 200px;
			height: 300px;
			background-color: skyblue;
			display: table-cell;
			vertical-align: bottom;
		}
		.line2 {
			width: 200px;
			height: 300px;
			background-color: gray;
			display: table-cell;
			vertical-align: bottom;
		}
		.line3 {
			width: 200px;
			height: 300px;
			background-color: lightgreen;
			display: table-cell;
			vertical-align: bottom;
		}
		
		.small {
			margin: 0 auto;
			width: 50px;
			height: 30px;
			border: 1px solid #999;
			background-color: wheat;
		}

		.medium {
			margin: 0 auto;
			width: 100px;
			height: 30px;
			border: 1px solid #999;
			background-color: wheat;
		}

		.large {
			margin: 0 auto;
			width: 150px;
			height: 30px;
			border: 1px solid #999;
			background-color: wheat;
		}
	</style>
</head>
<body>
	<div class="box">
		<div class="line1">
		</div>

		<div class="line2">
		</div>

		<div class="line3">
		</div>
	</div>
</body>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
	// 生成色块
	setTimeout(function(){
		$('<p>').addClass('small').appendTo($('.line1'))
	},1000)
	setTimeout(function(){
		$('<p>').addClass('medium').insertAfter($('p'))
	},1500)
	setTimeout(function(){
		$('<p>').addClass('large').insertAfter($('p:last'))
	},2000)
	// 移动色块
	// 最小色块移动到最右边
	setTimeout(function(){
		$('p:first').prependTo($('.line3'))
	},2500)
	// 中号色块移动到中间
	setTimeout(function(){
		$('p').eq(0).appendTo($('.line2'))
	},3000)
	// 最小色块移动到中间
	setTimeout(function(){
		$('p:last').prependTo($('.line2'))
	},3500)
	// 最大色块移动到最右边
	setTimeout(function(){
		$('p:eq(0)').appendTo($('.line3'))
	},4000)
	// 最小色块移动到最左边
	setTimeout(function(){
		$('p:eq(0)').appendTo($('.line1'))
	},4500)
	// 中号色块移动到最右边
	setTimeout(function(){
		$('p').eq(1).insertBefore($('p').eq(2))
	},5000)
	// 最小色块移动到最右边
	setTimeout(function(){
		$('p').eq(0).prependTo($('.line3'))
	},5500)
</script>
</html>

运行实例 »

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

效果图:

搜狗截图20180409102846.png

总结:

1.appendTo()

    语法:content.appendTo(target)

    参数:目标

    功能:插入到目标元素内容的后面

2.prependTo()

    语法:content.prependTo(target)

    参数:目标

    功能:插入到目标元素内容的前面

3.insertAfter()

    语法:content.insertAfter(target)

    参数:目标

    功能:插入到目标元素的后面

4.insertBefore()

    语法:content.insertBefore(target)

    参数:目标

    功能:插入到目标元素的前面

注意:appendTo()和prependTo()是目标元素内容前后

          insertAfter()和insertBefore()是目标元素前后

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学