jQuery在另一个事件侦听器之前绑定事件侦听器
使用该插件打开模态风格的内容容器,并尝试与Google Analytics的事件跟踪集成,以跟踪视频何时打开 问题是,当我jQuery在另一个事件侦听器之前绑定事件侦听器,jquery,Jquery,使用该插件打开模态风格的内容容器,并尝试与Google Analytics的事件跟踪集成,以跟踪视频何时打开 问题是,当我 $('a.videoClickListener').click(function(event){ console.log('here'); _gaq.push(['_trackEvent', 'Product Page', 'Video Open', '<?php echo $product->getNameWithManufacturer();
$('a.videoClickListener').click(function(event){
console.log('here');
_gaq.push(['_trackEvent', 'Product Page', 'Video Open', '<?php echo $product->getNameWithManufacturer(); ?>']);
});
$('a.videoClickListener')。单击(函数(事件){
console.log('here');
_gaq.push([''u trackEvent',产品页面,视频打开','');
});
事件永远不会被触发,因为它会阻止事件继续(非常正确,否则如果单击超链接,页面将发生更改)
Prettypto似乎没有提供“打开”回调函数,但如果提供了,我无论如何也不能使用它,因为Prettypto侦听器是在布局中的某个位置设置的(每当我们想使用Prettypto时,我们都使用rel=“prettypto”
,并通过URL传递参数,这是Prettypto推荐的方法)。我还想将产品详细信息传递给Analytics,排除在所有开幕活动中使用全球视频开幕听众的可能性
如何在将侦听器绑定到侦听器之前绑定我的侦听器?如果我必须使用
.unbind()
,然后绑定我的侦听器,然后重新绑定到,我如何才能解除绑定插件中指定的处理程序?如果使用unbind(),那么即使插件所做的绑定也将被解除绑定。理想情况下,您可以在任何其他绑定之前添加事件绑定,以便它首先执行
不幸的是,jQuery似乎没有包含任何这样的功能
但是,我编写了一个代码,它似乎起到了作用:
$.fn.preBind = function(type, data, fn) {
this.bind(type, data, fn);
var currentBindings = this.data('events')[type];
var currentBindingsLastIndex = currentBindings.length - 1;
var newBindings = [];
newBindings.push(currentBindings[currentBindingsLastIndex]);
$.each(currentBindings, function (index) {
if (index < currentBindingsLastIndex)
newBindings.push(this);
});
this.data('events')[type] = newBindings;
return this;
};
我使用的是Jonathan的preBind方法,稍加修改,它很好地完成了这个任务
$.fn.preBind = function (type, data, fn) {
this.each(function () {
var $this = $(this);
$this.bind(type, data, fn);
var currentBindings = $this.data('events')[type];
if ($.isArray(currentBindings)) {
currentBindings.unshift(currentBindings.pop());
}
});
return this;
};
必须更改上述代码中的一行以使其正常工作:
var currentBindings = $this.data('events',type); // var currentBindings = $this.data('events')[type];
这可能是因为我使用的是jquery 2.0.3,它适用于较旧和较新的
jquery
版本:
/**
* @link http://stackoverflow.com/a/26892146/655224
*/
jQuery.fn.getEvents = function() {
if (typeof(jQuery._data) == 'function') {
return jQuery._data(this.get(0), 'events') || {};
} else if (typeof(this.data) == 'function') { // jQuery version < 1.7.?
return this.data('events') || {};
}
return {};
};
jQuery.fn.preBind = function(type, data, fn) {
this.each(function () {
var $this = jQuery(this);
$this.bind(type, data, fn);
var currentBindings = $this.getEvents()[type];
if (jQuery.isArray(currentBindings)) {
currentBindings.unshift(currentBindings.pop());
}
});
return this;
};
/**
*@linkhttp://stackoverflow.com/a/26892146/655224
*/
jQuery.fn.getEvents=函数(){
if(typeof(jQuery.\u data)=‘函数’){
返回jQuery._数据(this.get(0),'events')|{};
}else if(typeof(this.data)='function'){//jQuery版本<1.7。?
返回此.data('events')|{};
}
返回{};
};
jQuery.fn.preBind=函数(类型、数据、fn){
这个。每个(函数(){
var$this=jQuery(this);
$this.bind(类型、数据、fn);
var currentBindings=$this.getEvents()[type];
if(jQuery.isArray(currentBindings)){
currentBindings.unshift(currentBindings.pop());
}
});
归还这个;
};
但是要注意,这个函数只能返回/预绑定jQuery本身设置的事件
特别感谢@jonathanconway和@Patrick…这里是使用更现代的.On()方法的解决方案变体
// Same as .on() but moves the binding to the front of the queue.
$.fn.priorityOn = function (type, selector, data, fn) {
this.each(function () {
var $this = $(this);
var types = type.split(" ");
for (var t in types) {
$this.on(types[t], selector, data, fn);
var currentBindings = $._data(this, 'events')[types[t]];
if ($.isArray(currentBindings)) {
currentBindings.unshift(currentBindings.pop());
}
}
});
return this;
};
用法就像
$(document).priorityOn("click blur change", ".some .selector input", function (e) {
// Your code.
});
谢谢你的回复。如何选择要解除绑定的事件?他们是怎么命名的?在PrettyTo中,侦听器作为匿名函数绑定。PrettyTo如何停止事件?如果你和插件都在监听同一个项目上的某种类型的事件,那么插件就无法停止触发你的绑定。听插件听的同一类型的事件,也听完全相同的元素。再次感谢。它似乎正在工作。。。我一定是在什么地方犯了另一个错误(从PHP传递到javascript时可能没有编码产品名称)代码>。注意“this”是实际的html元素,而不是jQuery对象。+1:代码是jonathanconway的一个等功能优化。在jQuery的较新版本中,
$this.data('events')[type]
不起作用。您必须使用$。\u数据(此“事件”)[type]
@RichardRout尝试以下解决方案以获取较新和较旧jQuery版本中的事件:未捕获类型错误:无法读取未定义(…)的属性“更改”尝试以下解决方案以获取较新和较旧jQuery版本中的事件:stackoverflow.com/a/26892146/655224未捕获类型错误:无法读取未定义(…)的属性“change”
$(document).priorityOn("click blur change", ".some .selector input", function (e) {
// Your code.
});