使用JavaScript替换锚定中的部分URL

使用JavaScript替换锚定中的部分URL,javascript,Javascript,我的页面上有以下锚定链接: <a href="https://example.com/sizeguide/brand.html" id="size-guide" target="_blank"><span class="pull-right">View Size Guide</span></a> 但是,我需要使用JavaScript,使用名为{{product.brand.domain}的把手函数,为不同品牌更改URL的品牌部分 我给了锚一个

我的页面上有以下锚定链接:

<a href="https://example.com/sizeguide/brand.html" id="size-guide" target="_blank"><span class="pull-right">View Size Guide</span></a>

但是,我需要使用JavaScript,使用名为
{{product.brand.domain}
的把手函数,为不同品牌更改URL的品牌部分

我给了锚一个唯一的ID,但我找不到一种方法来完成我需要的任务。

使用:

document.getElementById('size-guide').src='newUrl';

假设您想将brand.html更改为newbrand.html,然后执行以下操作

var href = document.getElementById("size-guide").getAttribute('href');    
href = href.replace(/(.*)\/.*(\.html$)/i, '$1/newbrand$2');    
document.getElementById("size-guide").setAttribute("href", href);

你在使用模板引擎?在这里发布你的代码!