zoukankan      html  css  js  c++  java
  • 利用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.

  • 相关阅读:
    高性能解决线程饥饿的利器 StampedLock
    月初刚拿到美团offer,新鲜的美团现场面试41题(三面技术+HR面)
    这两份Java“并发+异步”编程宝典,堪称编程界的“瑰宝”
    阿里架构师的学习笔记:多线程+JVM+Mysql+Redis+设计模式
    打造多模块+高可用+高扩展Spring Cloud版分布式电商项目源码分享
    想要彻底搞懂微服务架构必先学:SpringBoot+SpringCloud+docker
    厉害了!阿里P8手写《springboot 核心》PDF来了
    阿里P8熬夜总结Spring源码笔记,上线3分钟“全网跪求”
    UFLDL课程学习(二)
    UFLDL课程学习(一)
  • 原文地址:https://www.cnblogs.com/ys-wuhan/p/6506625.html
Copyright © 2011-2022 走看看