开发者

Change link to another link in Jquery

A site I am working on has links for add to cart I would like to change that link to point to different page how can I achieve this in jquery.

$(document).ready(function() {
    //alert('Welcome to StarTrackr! Now no longer under police …');
    $("a[href='http://www.somesite.com/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,<web::ISSUE开发者_JAVA技巧>,npus,$99.00,npcdn,$99.00']").attr('href', 'http://www.live.com/');
});

I am trying this got this idea from here How to change the href for a hyperlink using jQuery but it's not working for me any help is appreciated.


Use a selector with *:

$("a[href*='scAddItem']").attr('href', 'http://www.live.com/');

The links' href attribute will be changed for any link that contains scAddItem somewhere in its url. You can modify it for your exact string though.

More Readings:

  • XPath selectors
  • attr method


i'd suggest adding an id to that link so you can reference it directly, much faster and simpler than trying to match on its href:

<a id="cartLink" href="/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,,npus,$99.00,npcdn,$99.00">Add To Cart</a>

<script type="text/javascript">
  $("#cartLink").attr('href','http://www.live.com');
</script>


The code you posted looks right (although, I would give the <a> tag a ID attribute, so that you could avoid specifying the long search string.

<a id="MyLink" href='/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,<web::ISSUE>,npus,$99.00,npcdn,$99.00'> text text text </a>

<script>
$(document).ready(function() 
{ 
    $("#MyLink").attr('href', 'http://www.live.com/'); 
}); 
</script>


Try this:

$("a[href='/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,,npus,$99.00,npcdn,$99.00']").click(function() {
    $(this).attr('href', 'http://www.live.com/');
});

Or this if you could change your html.

$("a.mylink").click(function() {
    $(this).attr('href', 'http://www.live.com/');
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜