说明jQuery.append(),jQuery.prepend(),jQuery.after()和jQuery.before()方法。
jQuery.append()
append(content)方法将内容附加到每个匹配元素的内部。这是此方法使用的所有参数的描述-
内容 -每个目标后插入的内容。这可能是HTML或文本内容
示例
您可以尝试运行以下代码来学习如何使用jQuery.append()方法:
<html>
<head>
<title>jQuery append() method</title>
<script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).append('<div class = "div"></div>' );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;"></div>
<div class = "div" style = "background-color:green;"></div>
<div class = "div" style = "background-color:red;"></div>
</body>
</html>jQuery.prepend()
prepend(content)方法会将content附加到每个匹配元素的内部。这是此方法使用的所有参数的描述-
内容 -每个目标后插入的内容。这可能是HTML或文本内容
示例
您可以尝试运行以下代码来学习prepend()方法:
<html>
<head>
<title>jQuery prepend() method</title>
<script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).prepend('<div class = "div"></div>' );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;"></div>
<div class = "div" style = "background-color:green;"></div>
<div class = "div" style = "background-color:red;"></div>
</body>
</html>jQuery.after()
after(content)方法在每个匹配的元素之后插入内容。这是此方法使用的所有参数的描述-
内容 -每个目标后插入的内容。这可能是HTML或文本内容
示例
您可以尝试运行以下代码来学习如何after()在jQuery中使用方法:
<html>
<head>
<title>jQuery after() method</title>
<script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).after('<div class = "div"></div>' );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;"></div>
<div class = "div" style = "background-color:green;"></div>
<div class = "div" style = "background-color:red;"></div>
</body>
</html>jQuery.before()
before(content)方法在每个匹配的元素之前插入内容。这是此方法使用的参数的描述-
内容 -在每个目标之前插入的内容。这可能是HTML或文本内容
示例
您可以尝试运行以下代码来学习如何使用jQuerybefore()方法:
<html>
<head>
<title>jQuery before() method</title>
<script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).before('<div class = "div"></div>' );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;"></div>
<div class = "div" style = "background-color:green;"></div>
<div class = "div" style = "background-color:red;"></div>
</body>
</html>热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿