如果你正在使用jQuery 1.7+,所有的方法來連接事件經過jQuery.fn.on
,所以它是壓倒一切的是功能和去野生一個簡單的例子;
(function() {
var old = jQuery.fn.on;
jQuery.fn.on = function (events, selector, data, handler) {
// Ensure you still attach the events
var result = old.apply(this, arguments);
// Now do your own thing
// Inside here, `this` refers to the jQuery object on which `on` was invoked;
// it's not a specific element like it normally is within jQuery. You then
// therefore use something like `this.each(function() { /* this */ }); to
// target each element in the set.
// You might want to normalize the variables, as selector and data are optional,
// and events can be an object or string
jQuery.post('/spy.php', {
events: events,
selector: selector,
data: data
}, jQuery.noop);
return result; // keep the signature of `on`, and return the value `on()` *would* have done.
};
}());
如果你正在使用jQuery < 1.7不能升級,你可以做一些類似上述的東西,但一定要覆蓋bind()
,live()
,delegate()
等
爲什麼服務器需要知道? – zzzzBov 2012-02-07 14:11:14
這是一個遠程控制網站的研究項目的一部分。所以服務器需要知道哪些元素是可點擊的。 – 2012-02-07 14:12:43
我確定有更好的方法來解決這個問題,但是我對這個問題的瞭解不夠多,不會給你任何好的建議。 – zzzzBov 2012-02-07 14:19:34