【问题标题】:if an a link contain something in the href change the href to this如果链接在 href 中包含某些内容,则将 href 更改为此
【发布时间】:2011-05-18 00:17:44
【问题描述】:

我想检查 href 是否包含“即将推出”,如果它确实更改了 href 以转到我的产品页面:

$('a[href$="coming-soon"]').attr('href', '/products.aspx');

无法解决。

【问题讨论】:

    标签: jquery href javascript


    【解决方案1】:

    $= is "attribute-ends-with",使用*= for "attribute contains",像这样:

    $('a[href*="coming-soon"]').attr('href', '/products.aspx');
    

    【讨论】:

      【解决方案2】:

      使用contains selector

      $(document).ready(function(){
         $("a[href*='coming-soon']").attr('href', '/products.aspx');
      });
      

      Try it here!

      【讨论】:

        【解决方案3】:

        $= 表示“以”结尾。您想找到即将推出的包含(URL 中的任何位置)的链接吗?试试*= 而不是$=

        【讨论】:

          【解决方案4】:

          您的选择器当前是任何锚标记,其href coming-soon 结尾。包含选择器是*=:

          $('a[href*="coming-soon"]').attr('href', '/products.aspx');
          

          http://api.jquery.com/attribute-contains-selector/

          如果这不是问题,请确保在加载 DOM 时运行代码;通过将代码包装在$(document).ready()

          $(document).ready(function () {
              $('a[href*="coming-soon"]').attr('href', '/products.aspx');
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-14
            • 1970-01-01
            • 2013-03-30
            • 2014-03-24
            • 2013-09-09
            • 1970-01-01
            相关资源
            最近更新 更多