开发者

MooTools: Attaching event to multiple elements

I've got a jQuery routine I need to convert to MooTools, but I can't get it to work. Here's my jQuery version:

$(".google-analytics-link").click(function () {
    var href = $(this).attr("hre开发者_JAVA百科f");
    pageTracker._link(href);
    location.href = href;
    return false;
});

Here's my MooTools translation:

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

Doesn't seem to work though. I don't understand MooTools selectors. Any help, please?


You don't need to explicitly set the window's location when clicking the link already does it. Currently the code stops the native event, calls a method on the pageTracker object, then redirects to the location of the clicked link.

Google Analytics documentation for the _link method says that

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.

implying that you simply have to stop the click event, and call the _link method which will take care of the rest.

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);
});


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜