创建按钮,我点击鼠标

时间:2016-09-03 21:32:40

标签: javascript javascript-events

我想创建一个按钮,显示鼠标点击的数量(按钮应该出现在我点击的位置),10秒后删除按钮。这是我试图解决问题的代码:< / p>

<!DOCTYPE html> 
<html> 
    <head>
    </head> 
    <body>
    </body> 
    <script type ="text/javascript"> 
    counter = 1; 

    document.onclick = function(e){
        var button =document.createElement('button');

        button.style.position ="fixed";
        button.style.marginLeft= e.screenX;
        button.style.marginTop = e.screenY;
        button.label =x;

        document.body.appendChild(button);
        setTimeout(function() {
            document.body.removeChild(button);
        }, 1000;
    };
    </script> 
</html>

1 个答案:

答案 0 :(得分:1)

以下代码将实现您所需的功能!

&#13;
&#13;
<html>

<head>
</head>

<body>
</body>
<script type="text/javascript">
  counter = 1;

  document.onclick = function(e) {
    var button = document.createElement('button');
    button.style.position = 'fixed';
    button.style.left = e.pageX + 'px';
    button.style.top = e.pageY + 'px';
    button.innerHTML = counter;
    counter++;

    document.body.appendChild(button);

    setTimeout(function() {
      document.body.removeChild(button);
    }, 1000);
  };
</script>

</html>
&#13;
&#13;
&#13;