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

How to generate elements with jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-16 11:20:352907browse

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>

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!

Statement:
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
Previous article:How to use reactNext article:How to use react