为没有它的出站链接添加onClick

时间:2012-11-21 06:53:41

标签: javascript onclick

我想为bit.ly,j.mp和domain.com/go/类型的链接(在内容和评论中)添加onClick="",这些链接尚未包含,以便链接如下所示: / p>

<a href="http://domain.com/go/best-house/" rel="external nofollow" onClick="_gaq.push(['_trackEvent', 'click', 'ps', 'http://domain.com/go/best-house/']);">link text</a>

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用纯JavaScript:

window.onload = function() {

    var elements = document.getElementsByTagName('a'),
        addGaqPush = function() {
            _gaq.push(['_trackEvent', 'click', 'ps', 'event_house']);
    };
    for( var i = 0, len = elements.length; i < len; i++ ) {
        elements[i].onclick = addGaqPush;
    }
}