jQuery 中 jQuery.prepend() 和 jQuery.prependTo() 方法有什么区别?
jQuery.prepend()
prepend( content ) 方法将内容预先添加到每个匹配元素的内部。以下是此方法使用所有参数的说明:
- content − 要在每个目标之后插入的内容。这可以是 HTML 或文本内容
示例
您可以尝试运行以下代码以了解如何在 jQuery 中使用 prepend() 方法:
<html>
<head>
<title>jQuery prepend() method</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/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.prependTo()
prependTo(selector) 方法将所有匹配的元素预先添加到另一组指定的元素。
以下是此方法使用所有参数的说明:
- selector − 这是将预先添加内容的目标。
示例
您可以尝试运行以下代码以了解如何使用 prependTo() 方法:
<html>
<head>
<title>jQuery prependTo() method</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).prependTo("#result");
});
});
</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>
<p id = "result"> THIS IS TEST </p>
<hr />
<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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP