Make anchor links refer to the current page when using <base>

后端 未结 12 1166
情书的邮戳
情书的邮戳 2020-11-30 03:29

When I use the html tag to define a base URL for all relative links on a page, anchor links also refer directly to the base URL. Is there a way to

12条回答
  •  温柔的废话
    2020-11-30 04:15

    Building upon @James Tomasino answer, this one is slightly more efficient, solves a bug with double hashes in the url and a syntax error.

    $(document).ready(function() {
        var pathname = window.location.href.split('#')[0];
        $('a[href^="#"]').each(function() {
            var $this = $(this),
                link = $this.attr('href');
            $this.attr('href', pathname + link);
        });
    });
    

提交回复
热议问题