我想创建一个显示鼠标点击次数的按钮(该按钮应该出现在我点击的位置),并在 10 秒后删除该按钮。这是我尝试解决问题的代码:
<!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>
最佳答案
下面的代码将实现您想要的功能!
<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>
关于javascript - 在我点击鼠标的地方创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39311527/