它们是使用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") ); });