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

后端 未结 12 1170
情书的邮戳
情书的邮戳 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:02

    To prevent multiple # in URL:

             document.addEventListener("click", function(event) {
              var element = event.target;
              if (element.tagName.toLowerCase() == "a" && 
                element.getAttribute("href").indexOf("#") === 0) {
                my_href = location.href + element.getAttribute("href");
                my_href = my_href.replace(/#+/g, '#');
                element.href = my_href;
              }
            });
    

提交回复
热议问题