javascript-如何在单击时仅触发一次函数调用-我做错了什么?
我已将onclick事件处理程序附加到按钮。单击后,我想显示数据(待办事项列表)。问题是,每次单击该按钮时,我都会不断调用函数,将相同的元素添加到DOM中。我在网上找不到真正的解决方案,我试过的都没用。有什么想法吗?谢谢 另外,我尝试只使用javascript而不使用Jqueryjavascript-如何在单击时仅触发一次函数调用-我做错了什么?,javascript,Javascript,我已将onclick事件处理程序附加到按钮。单击后,我想显示数据(待办事项列表)。问题是,每次单击该按钮时,我都会不断调用函数,将相同的元素添加到DOM中。我在网上找不到真正的解决方案,我试过的都没用。有什么想法吗?谢谢 另外,我尝试只使用javascript而不使用Jquery window.onload = init; function init() { var button=document.querySelector("#showTodos"); button.oncli
window.onload = init;
function init() {
var button=document.querySelector("#showTodos");
button.onclick = showAll;
}
function showAll () {
var ul = document.getElementById("todoList");
var listFragment = document.createDocumentFragment();
var todos = LocalStorageHelper.read("todos") || [];
todos.map(function(item){
var li = create(item);
listFragment.appendChild(li);
})
ul.appendChild(listFragment);
}
//What I tried (and which doesnt work):
button.onclick = !showAll ? showAll : null;
// Also tried: defining a variable, setting boolean to false and insight the function to true. That doesnt work too, and even if it did, I would need to define a variable as a global variable which I wouldnt want.
var called=false;
button.onclick = !called ? showAll : null;
您可以删除处理程序或存储
数据属性
,以将其标记为已执行:
button.addEventListener('click', showAll);
function showAll() {
if (this.dataset.executed) return;
this.dataset.executed = "true";
.
.
// Execute logic
});
您可以删除处理程序或存储
数据属性
,以将其标记为已执行:
button.addEventListener('click', showAll);
function showAll() {
if (this.dataset.executed) return;
this.dataset.executed = "true";
.
.
// Execute logic
});
调用时删除处理程序
window.onload = init;
function init() {
var button=document.querySelector("#showTodos");
button.onclick = showAll;
}
function showAll () {
var ul = document.getElementById("todoList");
var listFragment = document.createDocumentFragment();
var todos = LocalStorageHelper.read("todos") || [];
todos.map(function(item){
var li = create(item);
listFragment.appendChild(li);
})
ul.appendChild(listFragment);
button.onclick = null;
}
调用时删除处理程序
window.onload = init;
function init() {
var button=document.querySelector("#showTodos");
button.onclick = showAll;
}
function showAll () {
var ul = document.getElementById("todoList");
var listFragment = document.createDocumentFragment();
var todos = LocalStorageHelper.read("todos") || [];
todos.map(function(item){
var li = create(item);
listFragment.appendChild(li);
})
ul.appendChild(listFragment);
button.onclick = null;
}
执行一次处理程序后,只需分离处理程序:
function showAll () {
this.removeEventListener("click", showAll);
//...
}
button.addEventListener("click", showAll);
如果您在迁移到jquery时使用它,它会更简单。照办
$(button).once("click", function showAll(){ /*...*/});
执行一次处理程序后,只需分离处理程序:
function showAll () {
this.removeEventListener("click", showAll);
//...
}
button.addEventListener("click", showAll);
如果您在迁移到jquery时使用它,它会更简单。照办
$(button).once("click", function showAll(){ /*...*/});
为什么不禁用或删除它呢?将事件侦听器功能附加到按钮;在这个监听器中,从按钮中删除事件监听器。为什么不禁用它,或者删除它呢?将事件监听器功能附加到按钮;在此侦听器中,从按钮中删除事件侦听器。