批改状态:未批改
老师批语:
以下代码主要实现了插入元素的功能,具体区别可见下面
<!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对象。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号