[英]how to make javascript load new data every time (without using cache data) when I redirect it to a handler(page) on mouse click
[英]JavaScript / HTML work with DOM, Button which count click and make new string every time when I click
我需要创建计数点击但每次创建新字符串的按钮。 我有这个函数,每次点击时都会计数,但我不明白,为什么在 HTML 中它每次计数为零,应该是这样的:(垂直每次点击都会产生更新计数的新字符串) 1 2 3 4 。 ..
<form action="#">
<input type="button" value="Count" onclick="add_count()">
</form>
function add_count() {
let integer = (function () {
let counter = 0;
return function() {return counter++;};
}());
let tag = document. createElement("p");
let text;
text = document. createTextNode(integer());
tag. appendChild(text);
let element = document. getElementsByTagName("body")[0];
element. appendChild(tag)};
您只需要将counter
初始化移到函数范围之外
我已经稍微调整了你的代码。
注意:尽量不要使用内联事件监听器,而是在 JavaScript 文件中使用事件监听器
let counter = 0; const button = document.querySelector('input'); function add_count() { let integer = (() => counter++); const tag = document.createElement("p"); const text = document.createTextNode(integer()); tag.appendChild(text); const element = document.body element.appendChild(tag) }; button.addEventListener('click', add_count)
<form action="#"> <input type="button" value="Count"> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.