Home > Web Front-end > JS Tutorial > How to generate elements with jquery

How to generate elements with jquery

coldplay.xixi
Release: 2020-11-30 11:22:10
Original
2950 people have browsed it

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.

How to generate elements with jquery

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=&#39;button&#39; value=&#39;按钮&#39;>").appendTo($("#dv"));
            });
            //元素创建的另一个方式
            $("#btn").click(function () {
                //通过innerHTML的方式创建元素---以字符串的方式
                $("#dv").html("<input type=&#39;button&#39; value=&#39;按钮&#39;>");
            });
        });
    </script>
</head>
<body>
<input type="button" value="创建一个按钮" id="btn"/>
<div id="dv">
    小苏凑合有点帅吧
</div>
</body>
</html>
Copy after login

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!

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