jQuery replacing relative links

后端 未结 6 1270
忘掉有多难
忘掉有多难 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 18:06

    This is not pretty, but it should work across browsers:

    $(document).ready(function() {
      $("a[href^='/']").each(function(){ 
        var cur_href = $(this).attr("href");
        if( cur_href.indexOf( "http" ) !== -1 ) {
            $(this).attr("href", cur_href);
        } else {
            $(this).attr("href", 'http://www.mysite.com'+cur_href);
        }  
      });
    });
    

提交回复
热议问题