0

我想为我页面上的每个外部链接(例如 Google)进行重定向

当您悬停链接时,它会显示真实的链接,但是当您单击它时,它会变成重定向页面。

请帮忙!!

谢谢。

4

2 回答 2

0

You simply have to use an event listener for the click on the link and change it's href attribute.

If you only want to do it for external links, it gets a bit more complicated. This example redirects all links beginning with http:

var redirect = 'http://redirect.foo?url=';
$('a[href^="http"]').on('click', function(e){
    $(this).attr('href',  redirect + $(this).attr('href'));
});

jsFiddle: http://jsfiddle.net/aev4m/1/

However, please don't use this for bad things. It's not userfriendly at all in my opinion.

于 2012-05-26T15:03:04.237 回答
0
$("a[href]").click(function() { window.location.href = "MyRedirectURL" + $(this).attr("href"); return false;} );

Added: only for external links

$("a[href]").each(function()
{
    if (this.hostname != location.hostname)
        $(this).click(function()
        {
            location.href = "MyRedirectURL" + $(this).attr("href");
            return false;
        });
});
于 2012-05-26T15:12:49.627 回答