Home >Web Front-end >JS Tutorial >How to generate elements with jquery
How to generate elements with jquery: 1. Add the code [$("html code")]; 2. Use the html function. The html function modifies the html code in the element and can create elements.
The operating environment of this tutorial: windows10 system, jquery2.2.4, this article is applicable to all brands of computers.
Recommended: "jquery video tutorial"
How to generate elements with jquery:
Create elements
Method 1: $("html code")
Method 2: Or use the html function. The html function is to modify the html code in the element, or you can also create the element
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>title</title> <style> div { width: 300px; height: 200px; border: 1px solid red; } </style> <script src="jquery-1.12.1.js"></script> <script> $(function () { //点击一个按钮,创建一个按钮 $("#btn").click(function () { $("<input type='button' value='按钮'>").appendTo($("#dv")); }); //元素创建的另一个方式 $("#btn").click(function () { //通过innerHTML的方式创建元素---以字符串的方式 $("#dv").html("<input type='button' value='按钮'>"); }); }); </script> </head> <body> <input type="button" value="创建一个按钮" id="btn"/> <div id="dv"> 小苏凑合有点帅吧 </div> </body> </html>
Related free learning recommendations: JavaScript(Video)
The above is the detailed content of How to generate elements with jquery. For more information, please follow other related articles on the PHP Chinese website!