Jquery:单击“创建”按钮后如何创建10个div [英] Jquery: how to create 10 divs after click on create button

查看:71
本文介绍了Jquery:单击“创建”按钮后如何创建10个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我身上有创建按钮,当我点击该按钮时,我需要动态创建10个div,并给出随机颜色和位置。 喜欢这个



我尝试过:



我试过这样,但点击创建按钮后,它会相互创建10个div,所以我只看到1个div。我的代码有什么问题?我需要排序div在不同的位置,而不是彼此。



I have Create button on the body, when i will click on that button, i need to create 10 divs dynamically, and give random color and position. Like this

What I have tried:

I have tried like this, but after click on Create button, it creates that 10 divs on each other, so i see only 1 div. What's wrong in my code? i need sort that divs in different position, not on each other.

$(document).ready(function(){
    $("#btn").click(function(){
    var r = parseInt(Math.random()*256);
    var g = parseInt(Math.random()*256);
    var b = parseInt(Math.random()*256);
    var color = `rgb(${r},${g},${b})`;
    var ww = $(window).width();
    var wh = $(window).height();
    var posx = (Math.random() * ww);
    var posy = (Math.random() * wh);
    var div=("<div></div>");
    for(var i = 0; i <= 10; i++){
      $(div).css({"background": color, "width":"50", "height":"50","position":"absolute", "top": posy, "left": posx}).appendTo("body");
    }
    })
});

推荐答案

(document).ready(function(){
(document).ready(function(){


(#btn)。click(function(){
var r = parseInt (Math.random()* 256);
var g = parseInt(Math.random()* 256);
var b = parseInt(Math.random()* 256);
var color =`rgb(
("#btn").click(function(){ var r = parseInt(Math.random()*256); var g = parseInt(Math.random()*256); var b = parseInt(Math.random()*256); var color = `rgb(


{r},


这篇关于Jquery:单击“创建”按钮后如何创建10个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆