2021-10-07

HTML 中<base >标签必须位于 head 元素内部。

通常情况下,浏览器会从当前页面中的 相对URL 变换成绝对URL。

<base href="http://www.h2sheji.com/" />

<a href="#maodian">锚点</a>

这样我们就无法在html中直接做锚点,否则就会跳转到其他页面:

http://www.h2sheji.com/#maodian

(像Discuz中头部就有个base)

这时需要使用JS来实现

 

<a href="javascript:GetMaoUrl('maodian');">锚点</a>

上面对于静态界面是没有问题的,但如果锚点是动态生成的,可以使用下面的

 

 

<a href="javascript:GetMaoUrl('maodian');">锚点</a>
<script type="text/javascript">
GetMaoUrl = function (maoid) {
location.href = location.href + '#' + maoid;
}
</script>

 

打赏

好文章,更需要你的鼓励

本文由 氢设计 创作,除注明转载/出处外,均为本站原创,转载前请务必署名

最后编辑时间为:2021-11-23 10:21:01

本文链接:https://www.h2sheji.com/show-51.html