Home >Web Front-end >JS Tutorial >Can jquery div add elements?
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.
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>
Rendering:
Example 2: prepend() method
<script> $(document).ready(function(){ $("#btn").click(function(){ $("div").prepend("<b>在开头追加文本</b>。 "); }); }); </script>
Rendering:
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!