I need to update href value using jquery

后端 未结 7 438
南笙
南笙 2021-01-15 02:35

I need to update href value thorughout the page using jquery. Say href=\"http://www.google.com?gsec=account\" should be changed to href=\"http://account.google.com?gsec=acco

相关标签:
7条回答
  • 2021-01-15 02:41

    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.

    0 讨论(0)
  • 2021-01-15 02:42

    If you are changing all the links then:

    $(document).ready(function() {
    $("a").attr("href", "http://account.google.com?gsec=account");
    });
    
    0 讨论(0)
  • 2021-01-15 02:50

    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 );
    
    0 讨论(0)
  • 2021-01-15 02:57

    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"]').

    0 讨论(0)
  • 2021-01-15 02:58

    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");
    
    0 讨论(0)
  • 2021-01-15 03:01

    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")
    
    0 讨论(0)
提交回复
热议问题