删除链接的某些部分's href值

remove some part of a link's href value

本文关键字:href 链接 些部 删除      更新时间:2023-09-26

我有一些与href属性的链接。

<a href="http://stackoverflow.com/...variable1.../">My Link</a>
<a href="http://stackoverflow.com/...variable2.../">My Link</a>
<a href="http://stackoverflow.com/...variable3.../">My Link</a>

我需要获取href值,删除http://stackoverflow.com并将链接转换为

<a href="/...variable1.../">My Link</a>
<a href="/...variable2.../">My Link</a>
<a href="/...variable3.../">My Link</a>

例如

$('a').each(function () {  
     var full_link = $('a').attr('href') ;
     var delete_part = 'http://stackoverflow.com' ;
     var output = full_link - delete_part ;
     $('a').attr('href',output);
});

你给我什么建议?

这里是:

$('a').each(function () {
    $(this).prop('href', $(this).prop('href')
        .replace(/^http:'/'/stackoverflow'.com/, ''));
});

希望得到帮助。

请将代码更新为以下

 var full_link = $('a').attr('href') ;
 var delete_part = 'http://stackoverflow.com' ;
 var output = full_link.replace(delete_part, "") ;

供参考-http://www.w3schools.com/jsref/jsref_replace.asp

试试这个:

它将删除你的域名,无论它是什么。

它将适用于所有域名,无需指定域名

$('a').each(function () {  
     var full_link = $('a').attr('href') ;
     var output =  full_link.replace(/http?:'/'/[^'/]+/i, "");
     $('a').attr('href',output);
});

试试这个。。

$('a').each(function() {  
     var full_link = $('a').attr('href');
     var delete_part = 'http://stackoverflow.com';
     var output = full_link.replace(delete_part,""); 
     $('a').attr('href',output);
});
$('a').each(function () {  
     var full_link = $('a').attr('href') ;
     var delete_part = 'http://stackoverflow.com' ;
     var output = full_link.replace(delete_part, "") ;
     $('a').attr('href',output);
});