JavaScript中沒有撤消或重做事件。如果你想要這樣的功能,你必須自己寫在JavaScript中或找到一個提供這種功能的庫。
如果您試圖捕捉所有可能的方式,即可以更改輸入控件,以便您可以立即看到此類更改,請查看以下示例代碼:http://jsfiddle.net/jfriend00/6qyS6/,該代碼爲輸入控件實現了更改回調。這段代碼並不是直接爲下拉菜單設計的,但由於它是一種輸入控件的形式,因此您可以調整此代碼爲下拉菜單創建自己的更改事件。
那麼,StackOverflow在他們的無限智慧是禁止我只發佈一個jsFiddle的引用,所以我必須粘貼在這裏的所有代碼(出於某種原因,jsFiddles單獨出來,而不是其他網站引用)。我不是代表這是一個確切的解決方案,但作爲一個模板,你可以使用如何檢測用戶更改輸入控制:
(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 + ")");
});
如果沒有別的,你可以檢查ctrl-z擊鍵,這是大多數系統上的撤銷序列。儘管如此,如果用戶在瀏覽器的菜單中執行了「編輯 - >撤消」,那麼也不知道是否會觸發。 – 2013-03-20 05:46:56
爲什麼你不能在這裏使用切換功能...... !! – EnterJQ 2013-03-20 05:47:23
@MarcB好點,捕捉擊鍵肯定會奏效。但是沒有辦法檢測'edit - > undo' – Ian 2013-03-20 05:48:43