繁体   English   中英

制作一个按钮来创建框

[英]Make a button to create boxes

每当用户单击按钮时,我都必须在div内创建框(这里的divbox )。 到目前为止,我已完成以下操作,但未创建任何框。

代码:

<!DOCTYPE html>
<html>
    <head>
        <style>
      .myDiv {
        height: 50px;
        width: 50px;
        border-color: blue;
          }
      #box {
        width: 700px;
        height: 700px;
        border: solid black;
      }
    </style>
    </head>
    <body>
        <h1>The onclick Event</h1>
        <button id ="theBoxes" >Creating boxes</button>
        <div id = "box"></div>
        <script>
        var x = document.getElementById("theBoxes");
        x.addEventListener("click", myFunction)
          function myFunction() {
            var box = document.createElement('div');
            box.classList.add('myDiv');
            document.body.appendChild(box); 
          }
          </script>
    </body>
</html>

 var x = document.getElementById("theBoxes"); x.addEventListener("click", myFunction) function myFunction() { var box = document.createElement('div'); box.classList.add('myDiv'); document.body.appendChild(box); }
 .myDiv { height: 50px; width: 50px; border: 1px solid blue; } #box { width: 700px; height: 700px; border: solid black; position: absolute; }
 <!DOCTYPE html> <html> <head> </head> <body> <h1>The onclick Event</h1> <button id ="theBoxes" >Creating boxes</button> <div id = "box"></div> </body> </html>

您的代码正在运行,不幸的是, myDiv中的border-color: blue不起作用,并且该框也添加到了 div #box之外。

因此,我通过在#box添加position: absolute和在.myDiv添加border: 1px solid blue来修改您的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM