通常,对于<input>
或类似的,您将使用change
事件。例如:
$('.only_class_element').on ("change", function (zEvent) {
// DO WHATEVER HERE.
} );
但是,如果元素实际上不是输入类型,或者元素本身是通过 AJAX 添加/删除/重新创建的,那么您必须使用间隔(或 Mutation Observers,但我不建议这样做)。顺便说一句,与未过滤的监视相比,时间间隔的侵入性/资源密集度要小得多mousemove
!
在破坏性 AJAX 情况下,使用waitForKeyElements(),如下所示:
// ==UserScript==
// @name _Monitor Cheshire-cat element for changes
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements (".only_class_element", fireOnChangedValue);
function fireOnChangedValue (jNode) {
var nodeVal = $.trim (jNode.val () );
var lastVal = jNode.data ("lastVal") || "";
if (nodeVal != lastVal) {
// DO WHATEVER WITH nodeVal HERE.
}
jNode.data ("lastVal", lastVal);
return true;
}