insertBefore 和 before
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>溫故而知“心”</title>
<style>
.wrapper {
border: 1px solid black;
width: 200px;
padding: 10px;
}
.wrapper div {
width: 200px;
height: 100px;
}
.wrapper .box1 {
background-color: #ff0000;
}
.wrapper .box2 {
background-color: #0000ff;
}
.content {
width: 200px;
height: 50px;
background-color: #ff00ff;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box1">box1</div>
<div class="box2">box2</div>
</div>
<div class="content">content</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.content').insertBefore('.box1');
$('.box1').before($('.content'));
</script>
</html>
insertAfter 和 after
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>溫故而知“心”</title>
<style>
.wrapper {
border: 1px solid black;
width: 200px;
padding: 10px;
}
.wrapper div {
width: 200px;
height: 100px;
}
.wrapper .box1 {
background-color: #ff0000;
}
.wrapper .box2 {
background-color: #0000ff;
}
.content {
width: 200px;
height: 50px;
background-color: #ff00ff;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box1">box1</div>
<div class="box2">box2</div>
</div>
<div class="content">content</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.content').insertAfter('.box1');
$('.box1').after($('.content'));
</script>
</html>
appendTo 和 append
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>溫故而知“心”</title>
<style>
.wrapper {
border: 1px solid black;
width: 200px;
padding: 10px;
}
.wrapper div {
width: 200px;
height: 100px;
}
.wrapper .box1 {
background-color: #ff0000;
}
.wrapper .box2 {
background-color: #0000ff;
}
.content {
width: 200px;
height: 50px;
background-color: #ff00ff;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box1">box1</div>
<div class="box2">box2</div>
</div>
<div class="content">content</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.content').appendTo('.wrapper');
$('.wrapper').append($('.content'))
</script>
</html>
prependTo 和 prepend
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>溫故而知“心”</title>
<style>
.wrapper {
border: 1px solid black;
width: 200px;
padding: 10px;
}
.wrapper div {
width: 200px;
height: 100px;
}
.wrapper .box1 {
background-color: #ff0000;
}
.wrapper .box2 {
background-color: #0000ff;
}
.content {
width: 200px;
height: 50px;
background-color: #ff00ff;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box1">box1</div>
<div class="box2">box2</div>
</div>
<div class="content">content</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.content').prependTo('.wrapper');
$('.wrapper').prepend($('.content'))
</script>
</html>
remove 和 detach
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>溫故而知“心”</title>
<style>
div {
width: 200px;
height: 200px;
background-color: #ff0000;
}
</style>
</head>
<body>
<div></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('div').click(function () {
alert('0')
})
</script>
</html>
$()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>溫故而知“心”</title>
<style>
div {
width: 200px;
height: 200px;
background-color: #ff0000;
}
</style>
</head>
<body>
<div></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
</script>
</html>