首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用javascript创建一个可点击的圆圈10次,如果我点击它,它会在中心显示我点击的次数

使用JavaScript创建一个可点击的圆圈10次,并在中心显示点击的次数,可以通过以下步骤实现:

  1. 创建一个HTML文件,命名为index.html,并在文件中添加一个空的div元素,用于显示圆圈和点击次数。例如:
代码语言:txt
复制
<!DOCTYPE html>
<html>
<head>
    <title>Clickable Circle</title>
    <style>
        #circle {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: blue;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div id="circle"></div>
    <script src="script.js"></script>
</body>
</html>
  1. 创建一个JavaScript文件,命名为script.js,并在文件中编写JavaScript代码。代码如下:
代码语言:txt
复制
var circle = document.getElementById("circle");
var count = 0;

circle.addEventListener("click", function() {
    count++;
    circle.innerHTML = count;
});

for (var i = 0; i < 10; i++) {
    var newCircle = document.createElement("div");
    newCircle.className = "circle";
    newCircle.style.left = Math.random() * 90 + "vw";
    newCircle.style.top = Math.random() * 90 + "vh";
    document.body.appendChild(newCircle);
}
  1. 将index.html和script.js文件放在同一个文件夹中,并在浏览器中打开index.html文件。你将看到一个蓝色的圆圈,点击它,中心会显示点击的次数。

这个例子中,我们使用JavaScript创建了一个可点击的圆圈,并通过事件监听器在每次点击时更新点击次数。同时,我们还使用了CSS样式来定义圆圈的外观。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券