jQuery - add/remove elements
jQuery - Adding Elements
With jQuery, it is easy to add new elements/content.
Add new HTML content
We will learn four jQuery methods for adding new content:
append() - Insert content at the end of the selected element
prepend() - Insert content at the beginning of the selected element
after() - Insert content after the selected element
before() - Insert before the selected element Content
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
function appendText(){
var txt1="<p>10月27日。</p>"; // 使用 HTML 标签创建文本
var txt2=$("<p></p>").text("星期四。"); // 使用 jQuery 创建文本
var txt3=document.createElement("p");
txt3.innerHTML="小雨。"; // 使用 DOM 创建文本 text with DOM
$("body").append(txt1,txt2,txt3); // 追加新元素
}
</script>
</head>
<body>
<p>这是一个段落。</p>
<button onclick="appendText()">追加文本</button>
</body>
</html>jQuery - Deleting Elements
With jQuery, you can easily delete existing HTML elements.
Delete elements/content
If you need to delete elements and content, you can generally use the following two jQuery methods:
remove() - delete the selected element (and its children Element) empty() - deletes sub-elements from the selected element
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").remove();
});
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">
这些是示例
<p>这些是示例</p>
<p>这些是示例</p>
</div>
<br>
<button>移除示例</button>
</body>
</html>Compare the two methods to distinguish usage.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").empty();
});
});
</script>
</head>
<body>
<div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">
这些是示例。
<p>这些是示例。</p>
<p>这些是示例。</p>
</div>
<br>
<button>清空示例</button>
</body>
</html>- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~ 















