jQuery中的jQuery add()和jQuery append()方法有什么区别?
jQueryadd()方法
如果要将元素添加到现有元素组中,请使用add()方法。
示例
您可以尝试运行以下代码来学习如何使用jQuery.add()方法:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h1").add("span").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Nhooo</h1>
<p>Text Tutorials for free.</p>
<span>Video Tutorials for free.</span>
</body>
</html>jQueryappend()方法
该append()方法将内容附加到每个匹配元素的内部。
示例
您可以尝试运行以下代码来学习如何使用jQueryappend()方法:
<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>热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短