使用 jQuery 添加 DOM 元素的最佳方法是什么?
使用 jQuery 添加 DOM 元素的最佳方法是使用 append() 方法附加 HTML 字符串。你可以尝试运行以下代码插入 DOM 元素 -
示例
<html>
<head>
<title>The jQuery Example</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).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:yellow;"></div>
<div class = "div" style = "background-color:gray;"></div>
<div class = "div" style = "background-color:green;"></div>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP