jQuery属性选择器不工作
我正在Google analytics中设置跟踪功能,用于当有人单击方向链接时,href将包含“maps.Google.com…”,因此我使用以下内容:jQuery属性选择器不工作,jquery,jquery-selectors,Jquery,Jquery Selectors,我正在Google analytics中设置跟踪功能,用于当有人单击方向链接时,href将包含“maps.Google.com…”,因此我使用以下内容: // track directions jQuery("a[href*='maps.google.com']").click(function(event) { console.log('directions link clicked'); if (typeof gtag !==
// track directions
jQuery("a[href*='maps.google.com']").click(function(event) {
console.log('directions link clicked');
if (typeof gtag !== 'undefined') {
gtag('event', 'Click', {
'event_category': 'Contact',
'event_label': 'Directions',
'event_callback': function() {
console.log("directions tracking sent successfully");
}
});
} // end if variable defined
不幸的是,这两个console.logs()都没有被触发
然而,以下工作:
jQuery("a[href^='mailto']").click(function(event) {
});
这可能是因为我的方向链接选择器包含“.”s?所以对我来说它不起作用,因为元素是由脚本创建的(用于WordPress的Google地图插件) 以下是我的工作解决方案:
// track directions
jQuery('body').on("click", "a[href*='maps.google.com']", function(event) {
if (typeof gtag !== 'undefined') {
gtag('event', 'Click', {
'event_category': 'Contact',
'event_label': 'Directions',
'event_callback': function() {
console.log("directions tracking sent successfully");
}
});
} // end if variable defined
}); // end click function
你能添加一个这个链接的例子吗?当然,例子会是,它在这里工作,也许有另一个代码导致了这个问题?嗯。。可能是因为我在新选项卡中打开了链接,而我没有阻止默认操作?