博主信息
博文 35
粉丝 2
评论 0
访问量 27441
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
JQ常见移动函数--2018年4月9日13时30分
小学僧的博客
原创
700人浏览过

以下代码主要实现了插入元素的功能,具体区别可见下面

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.box1 ul,.box2 ul{
			overflow: hidden;
		}
		.box1 li,.box2 li{
			list-style-type: none;
			width: 50px;
			height: 50px;
			background-color: lightgreen;
			border-radius: 50%;
			text-align: center;
			line-height: 50px;
			float: left;
			margin: 5px 20px;
		}
		.box2 li{
			background-color:lightblue;
		}
		.bar{
			width: 500px;
			height: 50px;
			background-color:#efefef; 
			text-align: center;
			display: table-cell;
			vertical-align: middle;
			border-radius: 5px;
		}
		.bar button{
			width: 80px;
			height: 30px;
			margin: 0px 5px;
			border: none;
			background-color: #B0C4DE;
			border-radius: 5px;
			font-size: 12px;
		}
		.bar button:hover{
			cursor: pointer;
		}
	</style>
</head>
<body>
	<div class="box1">
		<ul>
			<li>1</li>
			<li>2</li>
			<li>3</li>
			<li>4</li>
			<li>5</li>
		</ul>
	</div>		
	<div class="bar">
		<button>appendTo()</button>
		<button>prependTo()</button>
		<button>insertAfter()</button>
		<button>insertBefore()</button>
		<button>resert</button>
	</div>
	<div class="box2">
		<ul>
			<li>6</li>			
			<li>7</li>
			<li>8</li>
			<li>9</li>
			<li>10</li>
		</ul>
	</div>
</body>
</html>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
	$(function(){
		$('button:eq(0)').click(function(){
			$('.box2 li:nth-child(4)').appendTo($('ul:eq(0)')).css('background-color','red')
		})
		$('button:eq(1)').click(function(){
			 $('.box1 li:nth-of-type(2)').prependTo($('ul:eq(1)')).css('background-color','red')
		})
		$('button:eq(2)').click(function(){
			$('li:eq(5)').insertAfter($('li:eq(2)')).css('background-color','red')
		})
		$('button:eq(3)').click(function(){
			$('li:eq(6)').insertBefore($('li:eq(1)')).css('background-color','red')
		})					      
		$('button:eq(4)').click(function(){
			window.location.reload()			 
		})		
	})
</script>

运行实例 »

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

总结:四个函数的语法都是“ content.function(target) ”类型,content参数表示被移动的元素或者新建的元素,function代表函数名,target表示被插入的位置。

其中 appendTo()是将已有或新建元素插入到目标元素的内容的后面(内部插入),prependTo()是将已有或新建元素插入到目标元素的内容的前面(内部插入),insertAfter()是将已有或新建元素插入到目标元素的后面(外部插入),insertBefore()是将已有或新建元素插入到目标元素的前面(外部插入),返回值都是 移动或插入后的JQuery对象。

批改状态:未批改

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