开发者

I need to update href value using jquery

I need to update href value thorughout the page us开发者_如何学运维ing jquery. Say href="http://www.google.com?gsec=account" should be changed to href="http://account.google.com?gsec=account" how i can get this done.


This will do the replace throughout the page that I think you're looking for.

// Find `<a>` elements that contain `www.google.com` in the `href`.
$('a[href*="www.google.com"]').attr('href', function(i,href) {
       // return a version of the href that replaces "www." with "accounts."
    return href.replace('www.', 'accounts.');
});

Try it out: http://jsfiddle.net/dT8j6/


EDIT: This version allows for https:// and for links without the www..

Try it out: http://jsfiddle.net/dT8j6/1/

$('a[href*="google.com"]').attr('href', function(i,href) {
    return href.replace(/^http(s*):\/\/(www\.)*google.com/, 'http$1://accounts.google.com');
});

EDIT: If you only wanted to change elements that have gsec=account, then change the selector to $('a[href*="gsec=account"]').


lets say you have this:

<a href="http://www.ibm.com" id="myLink">

you should use this:

 var newHref = "http://google.com";

 $("#myLink").attr('href', newHref );


This is a duplicate of:

How to change the href for a hyperlink using jQuery

One scenario it doesn't mention is if you set an id on your anchor that needs to be changed then you can use the id as a selector in jQuery.

$("#LinkId").attr('href', "http://account.google.com?gsec=account")


This should hopefully provide a pretty full solution to your problem (as best I can interpret it, anyway):

$(function() {
  $('a').each(function() {
    var $this = $(this),
      href = $this.attr('href');
    var res = href.match(/(.*?)(www)(\.google\.com.*?)([?&]gsec=)([^&]+)(.*)/);
    if (null != res) {
      // remove the "full match" entry
      res = res.slice(1);
      // replace www match with account match
      res[1] = res[4];
      // update the href attribute
      $this.attr('href', res.join(''))
    }
  });
});


edit: If "account" is a static value, then this will work as well:

$(function() {
  $('a').each(function() {
    var $this = $(this),
      href = $this.attr('href');
    var res = href.match(/(.*?\/\/)(www)(\.google\.com.*?)([?&]gsec=account)(&?.*)/);
    if (null != res) {
      // remove the "full match" entry
      res = res.slice(1);
      // replace www match with account match
      res[1] = 'account';
      // update the href attribute
      $this.attr('href', res.join(''))
    }
  });
});

Please note that these solutions assume that there may be other variations in the URL, such as http/https and other query string variables.


I think you left something out in your question. Nevertheless:

var link = $("a"); // Or some other selector to access the link
link.attr("href", "http://account.google.com?gsec=account");


Use jQuery’s .attr() method. With one parameter, it returns the attribute value, with two parameters you set it.

$("a#whatever").attr("href", "http://account.google.com?gsec=account");


If you are changing all the links then:

$(document).ready(function() {
$("a").attr("href", "http://account.google.com?gsec=account");
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜