jQuery replacing relative links

后端 未结 6 1261
忘掉有多难
忘掉有多难 2020-12-19 17:33

Hopefully someone can explain some odd behavior I\'ve encountered with jQuery. The following script is looking for relative links on my page and replacing them with absolut

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 17:52

    try simply with

    $(document).ready(function() {
      $("a[href^='/']").each(function(){ 
        var cur_href = this.href;  /* <-- access to the href attribute 
                                          through the DOM reference */  
        $(this).prop("href", 'http://www.mysite.com'+cur_href);
      });
    });
    

提交回复
热议问题