jQuery 如何统计用户按下按钮的次数?

jqueryweb developmentfront end technology更新于 2026/1/6 4:22:17

要统计用户按下按钮的次数,需要在按钮点击时设置计数器。

示例

以下是代码 −

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
</head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<body>
<button id="pressButton">Press Me</button><br>
<h3>The current Value is= </h3>
<h1 id="incrementThisValue">0</h1>
</body>
<script>
var increment = 0;
var current = document.getElementById('incrementThisValue');
document.getElementById('pressButton').onclick = (event) => {
   current.textContent = ++increment;
};
</script>
</html>

要运行上述程序,请将文件名保存为 anyName.html (index.html)。右键单击该文件,然后选择"使用实时服务器打开"选项。在 VS Code 编辑器中。

输出

输出如下 −

按下按钮时,您将获得一个从 0,1,2,...N 开始的增量值

输出

输出如下 −


相关文章