Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在href中更改url的一部分_Javascript_Jquery - Fatal编程技术网

Javascript 在href中更改url的一部分

Javascript 在href中更改url的一部分,javascript,jquery,Javascript,Jquery,我在一个页面上有很多链接,这些链接是由一个web插件创建的: <a class="project-load" href="http://www.example.de/blog/portfolio/siteone/"></a> <a class="project-load" href="http://www.example.de/blog/portfolio/sitetwo/"></a> <a class="project-load"

我在一个页面上有很多链接,这些链接是由一个web插件创建的:

 <a class="project-load" href="http://www.example.de/blog/portfolio/siteone/"></a>
 <a class="project-load" href="http://www.example.de/blog/portfolio/sitetwo/"></a> 
 <a class="project-load" href="http://www.example.de/blog/portfolio/sitethree/"></a>
 ...
 <a class="project-load" href="http://www.example.de/blog/portfolio/sitfour/"></a>
 <a class="project-load" href="http://www.example.de/blog/portfolio/sitfive/"></a>

我发现了很多jQuery代码片段,它们可以更改整个URL。但我只需要在不同的例外情况下更改部分(sitefour、sitefive)。谢谢你的帮助

像这样的方法应该行得通


听起来简单的替换就可以了:

$('a').each(function(){
  $(this).attr('href', function(index,val){
    return val.replace('portfolio/','');
  });
});

使用正则表达式替换或
split
join
。您到底想要什么?你试图实现什么?最近不经常使用该网站,所有合理的关闭原因在哪里?!
// grab everything with class of project-load, loop over them
$('.project-load').each(function(){
  // grab the href
  var href = $(this).attr('href');
  // change the href to something else
  var newhref = href.replace(/blog\/portfolio\//, '');
  // update href
  $(this).attr('href', newhref);
});
$('a').each(function(){
  $(this).attr('href', function(index,val){
    return val.replace('portfolio/','');
  });
});