我有 textarea
并存储在数组 onclick 中,我需要从最后一个元素一一显示,并从用户点击的位置一一重做 onclick。我正在执行自定义撤消和重做功能。
var stack =[];
jQuery('#enter-text').keypress(function() {
console.log(jQuery('#enter-text').val());
stack.push(jQuery('#enter-text').val());
})
jQuery('#undo_text').click(function() {
console.log(stack.pop());
})
jQuery('#redo_text').click(function() {
// how to redo onclik where user undo text
})
我创建了jsfiddle
最佳答案
创建一个旧值数组,如下所示:
var deleted_stack = [];
// your other code
jQuery('#undo_text').click(function() {
deleted_stack.push(stack.pop());
})
jQuery('#redo_text').click(function () {
stack.push(deleted_stack.pop());
}
如果您也需要能够在文本中间执行此操作,则应该跟踪光标位置。那么你删除的堆栈应该看起来更像这样:
deleted_stack = [
{
char_idx: 2, // index of position in string where it was deleted, but
// then you shouldn't be deleting characters with .pop()
char_val: 'a'
},
... // other objects like first one
]
但是删除也变得更加复杂......
关于javascript - 如何在javascript中一一显示数组元素的onclick事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898377/