单击按钮时更改计数器的值

发布于 2025-01-11 15:16:53 字数 1144 浏览 0 评论 0原文

我已经搜索了一段时间,我发现我可以从 doGet 向创建的 html 发送一个值。

但我想做一些不同的事情,我想要一个按钮和标签,每次我按下该按钮时,它都会增加标签。

类似于按下该按钮的计数器。

我已经有了呈现按钮和标签的 HTML,这非常简单,

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
  <?!= include('stylesheet'); ?>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LDP Test</title>
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

  <script>
     function incrementCounter() {}
  </script>
</head>

<body>
  <label> Counter 0 </label>
  <button onClick="incrementCounter"> Increment counter</button>
</body>

</html>

我想将incrementCounter 函数链接到这个标签,或者任何可以完成的 html 元素,只要按下按钮,它就会递增并呈现在屏幕上 有人可以指导我完成这个吗?

I've been searching for a while, and I found that I can send a value from doGet to html created.

But I want to do smth different, I want to have a button and label where where every time I press on that button it increments the label.

something like a counter for pressing that button.

I already has the HTML that renders button and label it's pretty simple

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
  <?!= include('stylesheet'); ?>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LDP Test</title>
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

  <script>
     function incrementCounter() {}
  </script>
</head>

<body>
  <label> Counter 0 </label>
  <button onClick="incrementCounter"> Increment counter</button>
</body>

</html>

I want to link the incrementCounter function to this label, or whatever html element that can be done that whenever the button is pressed it gets incremented and rendered on the screen
can someone guide me to go through this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2025-01-18 15:16:53

您的代码中有一些错误。首先,它应该是onclick而不是onClick,并且该函数应该被调用为onclick="incrementCounter()"

其次,标签应该与输入、文本区域或选择元素一起使用。

现在来看代码

HTML

<p>Count: <span id="counter">0<span></p>

<button onclick="incrementCounter()">Increment Counter</button>

JS

function incrementCounter() {
   const counterElem = document.querySelector('#counter'),
         count = +counterElem.innerHTML;
   counterElem.innerHTML = count+1;
}

代码中发生的事情是我选择要显示计数的目标元素。然后,我获取其innerHTML 的值,并在counterElem.innerHTML 之前使用+ 将其解析为整数。然后我将值加 1 并将其设置为innerHTML

You have some errors in your code. Firstly it should be onclick not onClick and the function should be called as onclick="incrementCounter()".

Secondly, a label should be used with a input, textarea or select element.

Now coming to the code

HTML

<p>Count: <span id="counter">0<span></p>

<button onclick="incrementCounter()">Increment Counter</button>

JS

function incrementCounter() {
   const counterElem = document.querySelector('#counter'),
         count = +counterElem.innerHTML;
   counterElem.innerHTML = count+1;
}

What is happening inside the code is I'm selecting the target element where the count would display. Then I'm getting the value of its innerHTML and parsing it as an integer using + before the counterElem.innerHTML. Then I'm adding 1 to the value and setting it as the innerHTML

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文