我有一个外部脚本,我无法修改它。该脚本加载一个<a>
按钮,并添加一个 jQuery。单击它...并以“return false”结束。
我需要在这次点击时触发我自己的代码。当我加载页面时<a>
不存在,所以我需要使用.on('click')
绑定“活”。但看起来像.on('click')
在 .click“之后”加载,当他使用“return false”时,我的.on('click')
未加载。
所以问题是...我怎样才能触发我对这个动态加载的 a#btn 的点击,它已经有一个返回 false 的 .click 函数?
这是小提琴:http://jsfiddle.net/PLpqU/ http://jsfiddle.net/PLpqU/
这里是一个代码示例:
<div id="container"></div>
// I want this action to be executed on click, and the other too
// I can't use .click because on the "real" code, the a#btn is loaded after the page by another script
jQuery(document).on('click','a#btn',function(){
ga('send', 'event', { eventCategory: 'xxxx', eventAction: 'yyyy' });
}) ;
// http://www.xxxxxx.com/distant-script.js
// This one is binded in a script that I cannot edit :
// Just before it load a#btn on the page, and then bind .click on it
// as he return false, the other on('click') are not executed
jQuery('#container').append('<a id="btn" />') ;
jQuery('a#btn').click(function(){
// Some code stuff I need to be executed, but I can't manage
return false ;
}) ;
如您所知,目标是在远程脚本加载的链接按钮上触发 Google Analytics 事件。