gpt4 book ai didi

javascript - 如何处理 javascript 中的撤消/重做事件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:40 27 4
gpt4 key购买 nike

我正在尝试使用 Javascript 和 JQuery 检测表单输入的值何时发生变化。不幸的是,我发现 JQuery 的 $(elem).change() 不够用,因为它只在 elem 失去焦点时触发 change 事件。我必须立即知道表单输入值何时发生变化。为此,我将与输入值的可能变化相关的事件缩小为 keyuppastecut撤消重做。然而,javascript 和 JQuery 似乎都没有处理撤消或重做的方法。

var onChange = function ()
{
alert('Checking for changes...');
};

$(this).off('keyup').on('keyup', onChange);
$(this).off('paste').on('paste', onChange);
$(this).off('cut').on('cut', onChange);

$(this).off('undo').on('undo', onChange); // undo ?
$(this).off('redo').on('redo', onChange); // redo ?

我在 Javascript/JQuery 中用谷歌搜索了撤消/重做事件,但没有找到任何有用的信息。有人可以帮助处理撤消/重做事件吗?

最佳答案

javascript 中没有撤消或重做事件。如果您想要这样的功能,您要么必须自己用 javascript 编写,要么找一个提供这样功能的库。

如果您试图捕获输入控件可以更改的所有可能方式以便您可以立即看到这样的更改,那么请查看此示例代码:http://jsfiddle.net/jfriend00/6qyS6/它为输入控件实现了更改回调。此代码不是直接为下拉菜单设计的,但由于它是一种输入控件形式,您可能可以调整此代码来为下拉菜单创建您自己的更改事件。

好吧,StackOverflow 以其无限的智慧禁止我仅发布对 jsFiddle 的引用,因此我必须将所有代码粘贴到此处(出于某种原因,jsFiddle 被单独挑出,而不是其他 Web 引用)。我并不是将其表示为一个精确的解决方案,而是作为一个模板,您可以使用它来检测用户对输入控件的更改:

(function($) {

var isIE = false;
// conditional compilation which tells us if this is IE
/*@cc_on
isIE = true;
@*/

// Events to monitor if 'input' event is not supported
// The boolean value is whether we have to
// re-check after the event with a setTimeout()
var events = [
"keyup", false,
"blur", false,
"focus", false,
"drop", true,
"change", false,
"input", false,
"textInput", false,
"paste", true,
"cut", true,
"copy", true,
"contextmenu", true
];
// Test if the input event is supported
// It's too buggy in IE so we never rely on it in IE
if (!isIE) {
var el = document.createElement("input");
var gotInput = ("oninput" in el);
if (!gotInput) {
el.setAttribute("oninput", 'return;');
gotInput = typeof el["oninput"] == 'function';
}
el = null;
// if 'input' event is supported, then use a smaller
// set of events
if (gotInput) {
events = [
"input", false,
"textInput", false
];
}
}

$.fn.userChange = function(fn, data) {
function checkNotify(e, delay) {
// debugging code
if ($("#logAll").prop("checked")) {
log('checkNotify - ' + e.type);
}

var self = this;
var this$ = $(this);

if (this.value !== this$.data("priorValue")) {
this$.data("priorValue", this.value);
fn.call(this, e, data);
} else if (delay) {
// The actual data change happens after some events
// so we queue a check for after.
// We need a copy of e for setTimeout() because the real e
// may be overwritten before the setTimeout() fires
var eCopy = $.extend({}, e);
setTimeout(function() {checkNotify.call(self, eCopy, false)}, 1);
}
}

// hook up event handlers for each item in this jQuery object
// and remember initial value
this.each(function() {
var this$ = $(this).data("priorValue", this.value);
for (var i = 0; i < events.length; i+=2) {
(function(i) {
this$.on(events[i], function(e) {
checkNotify.call(this, e, events[i+1]);
});
})(i);
}
});
}
})(jQuery);

function log(x) {
jQuery("#log").append("<div>" + x + "</div>");
}

// hook up our test engine
$("#clear").click(function() {
$("#log").html("");
});


$("#container input").userChange(function(e) {
log("change - " + e.type + " (" + this.value + ")");
});

关于javascript - 如何处理 javascript 中的撤消/重做事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15516218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com