【问题标题】:jQuery replace href and change url in every <a>jQuery 替换 href 并更改每个 <a> 中的 url
【发布时间】:2013-03-05 07:45:57
【问题描述】:

我是 jQuery 新手,我正在尝试用另一个链接替换多个链接,但我似乎无法找到原因,请您帮帮我。

代码:

第一次尝试:

jQuery(document).ready(function() {

    $("a").each(function() { 

        newUrl += $(this).href+ textOfNew + " ";

        this.href = this.href.replace((this.href), newUrl);

    });

});

第二次尝试:

jQuery(document).ready(function() {

    $("a").each(function() { 
        $("$(this).href").val(function(i, val) {
            var newUrl = "test" ;

            return = $(this).href.replace($(this).href, newUrl);
        }); 
    });
}); 

【问题讨论】:

    标签: jquery url replace href


    【解决方案1】:
    jQuery(function($) {
        $("a").attr('href', function(i, href) {
            return href + '/test';
        });
    });
    

    FIDDLE

    【讨论】:

      【解决方案2】:
      $("a").each(function() { 
      
          newUrl += $(this).attr("href") + textOfNew + " ";
      
          $(this).attr("href", newUrl);
      
      });
      

      【讨论】:

        【解决方案3】:

        试试这个

           $("a").each(function() { 
        
                  var newUrl =  $(this).attr('href')+ textOfNew + " ";
        
                      $(this).attr('href',newUrl);
        
            });
        

        【讨论】:

        • 您不需要each(),只需将匿名函数传递给attr() 方法即可。作为@adeneo shows.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-20
        • 1970-01-01
        • 2018-09-22
        • 2011-08-18
        • 2019-10-06
        • 1970-01-01
        • 2015-06-20
        相关资源
        最近更新 更多