如果你正在使用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