如何在 jQuery 中创建一个 div 元素?
使用 jQuery 添加 div 的方法有很多,但由于要求是“单击”任何正方形,我们必须添加一个 div。 以下代码将帮助您单击添加 div
Click on any square below to see the result <div id='box'> <div class = "div" style = "background-color:blue;"></div> <div class = "div" style = "background-color:green;"></div> <div class = "div" style = "background-color:red;"></div> </div>
$(".div").on("click",function(){
$('#box').append(
$('<div/>')
.attr("id", "newDiv1")
.addClass("div")
.append("<span/>")
.text("hello world")
);
});
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP