如何将页面加载到锚标记?

时间:2013-05-03 21:55:15

标签: tags anchor

我怎么可能,或者是否可能在网页加载时这样做,它首先显示的是页面的一部分,你希望这个人首先看到锚标记。

有可能吗?

提前感谢您的所有帮助。

4 个答案:

答案 0 :(得分:9)

像这样简单的事情就可以了

<body onload=' location.href="#myanchor" '>
<a id='myanchor' href='#'>anchor text</a>

答案 1 :(得分:7)

使用javascript。

解决方案1 ​​

加载文档时,转到您想要的“哈希”或锚点

<script type="text/javascript">
function load()
{
window.location.hash="mylocation"; 
}
</script>

解决方案2

在标题中:

<script type="text/javascript" language="javascript">
    function moveWindow (){window.location.hash="mylocation";}
</script>

在身体标签上:

<body onload="moveWindow()">

你的主播:

<a name="mylocation"></a&GT;

答案 2 :(得分:4)

这是2018年的更新动态。

如果您想提取网址,请从网址中提取锚点,然后将网页移至该网页,您可以在网页加载时运行此网页。

if (window.location.href.indexOf('#') == 0) {
    let hash = window.location.href.split('#')[1];
    let hashTop = document.querySelector(`#${hash}`).offsetTop;
    window.scrollTop = hashTop;
}

还有这种方式更简洁。

if (location.hash) {
    let target = location.hash;
    window.scrollTop = document.querySelector(target).offsetTop;
}

如果您没有使用babel编译ES6,只需将let关键字更改为var即可。这也适用于WordPress,它会在锚之前的URL中添加斜杠。

答案 3 :(得分:0)

混合使用这些答案对我来说是最好的选择,并且具有动态性的优势,它可以从URL的锚点中拉出,并强制页面加载到预期的目的地:

proc sql;
  create table summary_array_way as
  select segment, position, count(*) as count
  from 
    ( select * from combinations_array_way union all corresponding
      select * from each_position
    )
  group by segment, position;
quit;

proc transpose data=summary_array_way out=want_array_way(drop=_name_ where=(not missing(segment))) prefix=count_;
  by segment;
  id position;
  var count;
  format count 6.;
run;

这是我项目中最干净的解决方案,原因是其他许多事情在onload下运行:

jQuery:

if (location.hash) location.href = location.hash;

香草:

$(window).on("load", function() {
    if (location.hash) location.href = location.hash;
}