jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery绑定事件侦听器之前的另一个大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用 prettyPhoto插件打开模态样式的内容容器,并尝试与Google Analytics(分析)的事件跟踪集成,以跟踪视频何时打开.

麻烦是,当我

$('a.videoClickListener').click(function(event){
    console.log('here');
    _gaq.push(['_trackEvent','Product Page','Video Open','<?PHP echo $product->getNameWithManufacturer(); ?>']);
});

事件永远不会被触发,因为prettyPhoto阻止事件继续(非常正确地,否则如果点击超链接,页面会改变).

prettyPhoto似乎并​​没有提供一个’开放’回调函数,但是如果我无法使用它,因为prettyPhoto监听器设置在布局的某个地方(我们每次要使用rel =“prettyPhoto”) prettyPhoto,并通过URL传递参数,这是prettyPhoto推荐的做事方式).我也想将产品细节传递给Google Analytics(分析),排除了所有prettyPhoto开幕活动中的全球视频开放听众.

如何在prettyPhoto侦听器之前绑定我的监听器?如果事实证明,我必须使用.unbind(),然后绑定我的监听器,然后重新绑定prettyPhoto,我如何解除绑定在插件中指定的处理程序?

解决方法

理想情况下,您可以在任何其他方式之前添加事件绑定,以便先执行事件绑定.

不幸的是,jQuery似乎没有包含任何这样的设施.

不过,我已经编写了一个preBind method,这似乎是诀窍:

$.fn.preBind = function(type,data,fn) {
  this.bind(type,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;
};

用法

$('#button').bind('click',function() {
  console.log('world');
});

// 2nd apostrophe for the SELEctor was missing
$('#button').preBind('click',function() {
  console.log('Hello');
});

$('#button').click();

// Output:
//
// > "Hello"
// > "world"

大佬总结

以上是大佬教程为你收集整理的jQuery绑定事件侦听器之前的另一个全部内容,希望文章能够帮你解决jQuery绑定事件侦听器之前的另一个所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。