开发者

How Replace some url by ID with Javascript

<a href='http://www.domain.com' id='replace' style='text-decoration:none;color:black;font-size:10px;'>This is text link</a>

<script language="javascript">
var n开发者_如何学编程ewURL = "mydomain.com/?refid=4877";
onload=function() {
    var dt = document.getElementById("replace");
    document.body.innerHTML = dt.getAttributeNode("href").value.replace(/domain.com/g,newURL);
}   


Just assign to the href attribute:

dt.href = dt.href.replace(/domain.com\/?/, newURL);

The optional trailing slash caters for browsers that automatically add a slash to hrefs.


You should probably use JQuery:

$(document).ready(function(){
    $("#replace").attr("href","http://www.mydomain.com/?refid=4877");   
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜