2013-07-29 136 views
1

我需要一个代码来代替我的<div>中的href或完整的html文档(与其相同)。替换锚元素中的href属性

我的内容是这样的:

<div class="entry-content post_content"> 
<center><a href="http://misite.com/go/redirect.php?q=post title" target="_blank"><img src="http://mysite.com/img/redirect.png" class="attachment-medium" alt="redirect-150x150-700x30" width="700" height="30"></a></center> 
<!---Content--> 
</div> 

我需要改变_http://mysite.com/go/到http:// mynewsite的.com /走/

任何帮助?什么代码应该是?,即时搜索stackoverflow但没有线程解决我的问题。

+0

什么时候该更改生效?点击按钮(或)页面加载(或)? – Harry

+0

和一个链接或所有链接或...? – Mathletics

+0

所有链接和页面加载 –

回答

0

我假设你有一些前端(CMS?)访问和没有直接文件访问。

你可以把它放在每个文件的头部。

function fixLinks(){ 
    var links = document.getElementsByTagName('a');//Get all links 
    for(i = 0 ; i<links.length ; i++){//Loop throught links 
    var curLink = links[i].href // Cache 
    links[i].href = curLink .replace('mysite','mynewsite');//Replace mysite with my newsite and return the fixed string 
    } 
    } 
window.onload = fixLinks; 
+0

只有在将它放在标题中时才有用?我使用wordpress,并且我希望把它放在一个小部件中。 –

+0

Widget就好了 – raam86

+0

更新了更全面的解决方案 – raam86