**我知道我可以使用Jquery或javascript在全站点更改网址。 用:
$.each($('a'), function(i,v){
var oldULR = $(v).attr('href');
var newURL = oldULR.replace('/fold/fold', '/newfolder');
$(this).attr('href', newURL);
});
但是,如果网址末尾没有add
或delete
,我只会尝试更改网址。我有以下网址示例:**
<a href="http://example.com/fold/fold/AAA">AAA</a>
<a href="http://example.com/fold/fold/AAA/add">Add new AAA</a>
<a href="http://example.com/fold/fold/BBB/delete">Delete BBB</a>
<a href="http://example.com/fold/fold/CCC/add">Add new CCC</a>
**所以我只想将/fold/fold
更改为/newfolder
,如果没有add
或delete
的结尾。 / p>
所以最终的网址看起来像**:
<a href="http://example.com/newfolder/AAA">AAA</a> <!--changed-->
<a href="http://example.com/fold/fold/AAA/add">Add new AAA</a> <!--no change-->
<a href="http://example.com/fold/fold/BBB/delete">Delete BBB</a> <!--no change-->
<a href="http://example.com/fold/fold/CCC/add">Add new CCC</a> <!--no change-->
答案 0 :(得分:1)
使用条件语句可以做到这一点
$.each($('a'), function(i,v){
var oldURL = $(v).attr('href');
if (!oldURL.endsWith('/add') && !oldURL.endsWith('/delete')) {
var newURL = oldULR.replace('/fold/fold', '/newfolder');
$(this).attr('href', newURL);
}
});
答案 1 :(得分:0)
也许使用.endsWith()来检查URL的结束字符串?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
$.each($('a'), function(i,v){
var oldULR = $(v).attr('href');
var newURL = oldULR.replace('/fold/fold', '/newfolder');
if (!oldULR.endsWith('/add') && !oldULR.endsWith('/delete')){
$(this).attr('href', newURL);
}
});