Home > Web Front-end > JS Tutorial > Can jquery div add elements?

Can jquery div add elements?

藏色散人
Release: 2020-12-16 10:39:14
Original
2032 people have browsed it

jquery div can add elements. The adding method is: 1. Insert content at the end of the selected element through the append method; 2. Insert content at the beginning of the selected element through the prepend method.

Can jquery div add elements?

The operating environment of this tutorial: Windows 7 system, jquery version 1.10.0. This method is suitable for all brands of computers.

Recommended: "jquery Video Tutorial"

Through jQuery, you can easily add new elements/content.

Method:

append() - Insert content at the end of the selected element

prepend() - Insert content at the beginning of the selected element

Example 1: Using append()

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
$("div").append(" <b>插入文本</b>.");
});
});
</script>
</head>
<body>
<div>这是一个div文本。</div>
<div>这是另一个div文本</div><br>
<button id="btn">插入文本</button>
</body>
</html>
Copy after login

Rendering:

Can jquery div add elements?

Example 2: prepend() method

<script>
$(document).ready(function(){
$("#btn").click(function(){
$("div").prepend("<b>在开头追加文本</b>。 ");
});
});
</script>
Copy after login

Rendering:

Can jquery div add elements?

The above is the detailed content of Can jquery div add elements?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template