Replace link location (jQuery)
<a href="http://www.google.com">link</a>
How do I replace link location from google.com to say "#"? $('a').attr( 'href', '#' );
isn't working.
It works for me.
Test code:
<a id="MyLink" href="test.html">
jQuery:
$("#MyLink").attr("href", "#");
alert($("#MyLink").attr("href")); //alerts "#"
Is it possible you are trying to do this before the DOM has loaded?
Also, what browsers are you using?
EDIT:
To ensure this is only done when the DOM is loaded completely, use the document .ready()
function:
$(document).ready(function(){
$("#MyLink").attr("href", "#");
//other initialisation, e.g. event binding
});
Take a look at this question - How to change the href for a hyperlink using jQuery
精彩评论