单击按钮时更改计数器的值
我已经搜索了一段时间,我发现我可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有一些错误。首先,它应该是
onclick
而不是onClick
,并且该函数应该被调用为onclick="incrementCounter()"
。其次,标签应该与输入、文本区域或选择元素一起使用。
现在来看代码
HTML
JS
代码中发生的事情是我选择要显示计数的目标元素。然后,我获取其innerHTML 的值,并在
counterElem.innerHTML
之前使用+
将其解析为整数。然后我将值加 1 并将其设置为innerHTMLYou have some errors in your code. Firstly it should be
onclick
notonClick
and the function should be called asonclick="incrementCounter()"
.Secondly, a label should be used with a input, textarea or select element.
Now coming to the code
HTML
JS
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 thecounterElem.innerHTML
. Then I'm adding 1 to the value and setting it as the innerHTML