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.

  • 相关阅读:
    SQL 取日期
    myeclipse 8.5 安装jbpm3.2开发插件
    持续感悟
    程序员应该读的书与经常上的网站
    java连接ms sql server各类问题解析
    怎么实现Redis的高可用?(主从、哨兵、集群)
    Web系统突然爆”Asp.net ajax客户端框架未能加载“的一种可能原因(误改服务器系统时间)
    【转】Skyline软件介绍
    ArcSDE启动遇到ORA12560: TNS: 协议适配器错误解决办法
    开放源代码GIS资源集锦
  • 原文地址:https://www.cnblogs.com/ys-wuhan/p/6506625.html
Copyright © 2011-2022 走看看