javascript 中没有撤消或重做事件。如果你想要这样的功能,你要么必须自己用 javascript 编写它,要么找到一个提供这种功能的库。
如果您试图捕获可以更改输入控件的所有可能方式,以便您可以立即看到此类更改,请查看此示例代码:http: //jsfiddle.net/jfriend00/6qyS6/它实现了更改输入控件的回调。此代码不是直接为下拉菜单设计的,但由于它是一种输入控件形式,您可以调整此代码来为下拉菜单创建自己的更改事件。
好吧,StackOverflow 以其无限的智慧禁止我仅发布对 jsFiddle 的引用,因此我必须将所有代码粘贴到此处(出于某种原因,jsFiddles 被单独列出,而不是其他 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 + ")");
});