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 href
s.
You should probably use JQuery:
$(document).ready(function(){
$("#replace").attr("href","http://www.mydomain.com/?refid=4877");
});
精彩评论