所以我有一个像这样的链接:
<a href="http://thissite.org/thisfolder/21/thispage.php">
我想做的是修改它但保留其中的一部分,例如:
<a href="http://thissite.org/thatfolder/21/thatpage.php">
可以用 Jquery 或 js 来完成吗?
我知道我可以用 jquery 替换 href 属性,但我需要保留 url 的一部分(“21”),只更改其前后的文本。
我在想也许可以获取 href 属性,将其粘贴在变量中,然后将其拆开,然后以某种方式将其重新组合在一起。
对此的任何帮助将不胜感激。
最佳答案
您可以在属性 setter 函数中调用replace
:
$('a').attr('href', function(index, old) {
return old.replace(/thisfolder/i, 'thatfolder');
});
关于javascript - 修改href属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8794505/