开发者

Replace link location (jQuery)

开发者 https://www.devze.com 2022-12-17 22:37 出处:网络
开发者_JAVA技巧<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.
开发者_JAVA技巧
<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

0

精彩评论

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

关注公众号