利用jquery修改href的部分字符

试了好久

 

就一句代码即可。

1 $(document).ready(function(){
2     $('a').each(function(){
3         this.href = this.href.replace('ys_yinqin', 'others');
4     });
5 });

 

 

如果是替换href整个字符有以下方法:

 

来源于:http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery/28016553#28016553

Even though the OP explicitly asked for a jQuery answer, you don't need to use jQuery for everything these days.

 

1.纯 javascript写的

A few methods without jQuery:

  • If you want to change the href value of all <a> elements, select them all and then iterate through the nodelist(example)

    var anchors = document.querySelectorAll('a');
    Array.prototype.forEach.call(anchors, function (element, index) {
        element.href = "http://stackoverflow.com";
    });
  • If you want to change the href value of all <a> elements that actually have an hrefattribute, select them by adding the [href] attribute selector (a[href]): (example)

    var anchors = document.querySelectorAll('a[href]');
    Array.prototype.forEach.call(anchors, function (element, index) {
        element.href = "http://stackoverflow.com";
    });
  • If you want to change the href value of <a> elements that contain a specific value, for instance google.com, use the attribute selector a[href*="google.com"](example)

    var anchors = document.querySelectorAll('a[href*="google.com"]');
    Array.prototype.forEach.call(anchors, function (element, index) {
        element.href = "http://stackoverflow.com";
    });

    Likewise, you can also use the other attribute selectors. For instance:

    • a[href$=".png"] could be used to select <a> elements whose href value ends with .png.

    • a[href^="https://"] could be used to select <a> elements with href values that are prefixed with https://.

  • If you want to change the href value of <a> elements that satisfy multiple conditions: (example)

    var anchors = document.querySelectorAll('a[href^="https://"], a[href$=".png"]');
    Array.prototype.forEach.call(anchors, function (element, index) {
        element.href = "http://stackoverflow.com";
    });

..no need for regex, in most cases.

 

2.jquery写的

$("a").attr("href", "http://www.google.com/")

...Will modify the href of all hyperlinks to point to Google. You probably want a somewhat more refined selector though. For instance, if you have a mix of link source (hyperlink) and link target (a.k.a. "anchor") anchor tags:

<a name="MyLinks"></a>
<a href="http://www.codeproject.com/>The CodeProject</a>

...Then you probably don't want to accidentally add href attributes to them. For safety then, we can specify that our selector will only match <a> tags with an existing href attribute:

$("a[href]") //...

Of course, you'll probably have something more interesting in mind. If you want to match an anchor with a specific existing href, you might use something like this:

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

This will find links where the href exactly matches the string http://www.google.com/. A more involved task might be matching, then updating only part of the href:

$("a[href^='http://stackoverflow.com']")
   .each(function()
   { 
      this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/, 
         "http://stackoverflow.com");
   });

The first part selects only links where the href starts with http://stackoverflow.com. Then, a function is defined that uses a simple regular expression to replace this part of the URL with a new one. Note the flexibility this gives you - any sort of modification to the link could be done here.

posted @ 2017-03-05 20:37  一个勤奋的胖子  阅读(736)  评论(0编辑  收藏  举报