如何使用javascript/jquery在单击时更改任何本地链接的目标?

如何使用javascript/jquery在单击时更改任何本地链接的目标?,javascript,jquery,Javascript,Jquery,我需要更改任何本地链接的href,当它被点击时。 我已经知道如何选择正确的链接并创建新的url,但不知道如何更改位置。我需要确定任何其他点击事件也已运行,因此我不能立即更改位置 var my_host = 'example.com'; $(document).click(function(e) { if(e.target.hostname == my_host) { alert(e.target.protocol + "//" + e.target.hostnam

我需要更改任何本地链接的href,当它被点击时。 我已经知道如何选择正确的链接并创建新的url,但不知道如何更改位置。我需要确定任何其他点击事件也已运行,因此我不能立即更改位置

var my_host = 'example.com';
$(document).click(function(e) {
    if(e.target.hostname == my_host)
    {
        alert(e.target.protocol + "//" + e.target.hostname + "/#" + e.target.pathname + e.target.search);
        //...
    }
});

这与我的相关。

您可以像这样在本地链接上绑定事件,并使用
attr
方法更改
href

$('a[href*="yourdomain.com"]').click(function(){
  // your code before changing href
  $(this).attr('href', 'new url here');
});

yourdomain.com
替换为您自己的域,以便上述代码仅针对您的本地链接。

您可以像这样在本地链接上绑定事件,并使用
attr
方法更改
href

$('a[href*="yourdomain.com"]').click(function(){
  // your code before changing href
  $(this).attr('href', 'new url here');
});

yourdomain.com
替换为您自己的域,以便上述代码仅针对您的本地链接。

这是完全未经测试的,但应该可以帮助您实现以下目标:

var site = "http://yourdomain.com";

// Match anchor tags with an href value that starts with your
// site URL, or that start with / which are relative URLs (also
// your site).
$('a[href^="' + site + '"] a[href^="/"]').click(function(){
    var url = $(this).attr('href');
    if (url.indexOf(site) != -1) {
        url = site + "/#" + url.replace(site, ""); // Quick dirty way to get the path component of the URL
    } else {
        url = "/#" + url;
    }
    $(this).attr("href", url);
});

请注意,我阅读了您的另一个问题,我个人不明白您为什么要在单击事件中执行此操作,而不是在页面加载后立即替换所有href值。

这是完全未经测试的,但应该可以帮助您实现以下目标:

var site = "http://yourdomain.com";

// Match anchor tags with an href value that starts with your
// site URL, or that start with / which are relative URLs (also
// your site).
$('a[href^="' + site + '"] a[href^="/"]').click(function(){
    var url = $(this).attr('href');
    if (url.indexOf(site) != -1) {
        url = site + "/#" + url.replace(site, ""); // Quick dirty way to get the path component of the URL
    } else {
        url = "/#" + url;
    }
    $(this).attr("href", url);
});

请注意,我读了你的另一个问题,我个人不明白为什么你会在点击事件中这样做,而不是在页面加载后立即替换所有href值。

请归功于Sarfraz:以下是没有错误的答案

var my_host = "example.com";
$('a[href^="/"], a[href^="http://' + my_host + '"], a[href^="https://' + my_host + '"]').click(function(e){
    $(this).attr('href', e.target.protocol + "//" + e.target.hostname + "/#" + e.target.pathname + e.target.search);
});

这是没有bug的答案

var my_host = "example.com";
$('a[href^="/"], a[href^="http://' + my_host + '"], a[href^="https://' + my_host + '"]').click(function(e){
    $(this).attr('href', e.target.protocol + "//" + e.target.hostname + "/#" + e.target.pathname + e.target.search);
});

谢谢您已经回答了我的问题,并缩短了代码。但是,您还引入了一个bug;如果域位于路径的其他位置,例如作为查询字符串中的返回url,这将被错误触发。@systemicmultiple:您可以使用
document.location.search
查找url内容并相应地提取该部分。谢谢。您已经回答了我的问题,并缩短了代码。但是,您还引入了一个bug;如果域位于路径的其他位置,例如作为查询字符串中的返回url,这将被错误触发。@SystemicPlural:您可以使用
document.location.search
查找url内容并相应地提取部分。这不适用于https链接。之所以使用onclick,是因为更换一个链接比更换几十个链接要少得多。另外,在我的例子中,内容是动态加载的,所以单一的解决方案是理想的。前面的答案1)不起作用,2)没有考虑相对URL。您可以在我的代码中添加几个字符来处理https-hahahp.S。不确定您在做什么,但如果希望搜索引擎爬行器为正确的URL编制索引,则需要更改所有URL(不仅仅是单击的URL)。这不适用于https链接。之所以使用onclick,是因为更换一个链接比更换几十个链接要少得多。另外,在我的例子中,内容是动态加载的,所以单一的解决方案是理想的。前面的答案1)不起作用,2)没有考虑相对URL。您可以在我的代码中添加几个字符来处理https-hahahp.S。不知道你在做什么,但是如果你想让搜索引擎蜘蛛为正确的URL建立索引,你需要改变所有的URL(不仅仅是点击的URL)。