gpt4 book ai didi

javascript - MooTools:将事件附加到多个元素

转载 作者:行者123 更新时间:2023-11-29 20:21:46 24 4
gpt4 key购买 nike

我有一个需要转换为 MooTools 的 jQuery 例程,但我无法让它工作。这是我的 jQuery 版本:

$(".google-analytics-link").click(function () {
var href = $(this).attr("href");
pageTracker._link(href);
location.href = href;
return false;
});

这是我的 MooTools 翻译:

$$(".google-analytics-link").addEvent("click", function () {
var href = this.get("href");
pageTracker._link(href);
location.href = href;
return false;
});

不过好像不行。我不明白 MooTools 选择器。有什么帮助吗?

最佳答案

当单击链接时,您不需要明确设置窗口的位置。目前,代码会停止 native 事件,调用 pageTracker 对象上的方法,然后重定向到单击链接的位置。

_link 的 Google Analytics 文档方法说

This method works in conjunction with the _setDomainName() and _setAllowLinker() methods to enable cross-domain user tracking. The _link() method passes the cookies from this site to another via URL parameters (HTTP GET). It also changes the document.location and redirects the user to the new URL.

暗示您只需停止点击事件,并调用 _link 方法,它将处理其余部分。

var analyticsLinks = document.getElements('.google-analytics-link');

analyticsLinks.addEvent('click', function(event) {
// stop the page from navigating away
event.stop();
var href = this.get('href');
// let the Analytics API do its work, and then redirect to this link
pageTracker._link(href);
});

关于javascript - MooTools:将事件附加到多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3670813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com