JavaScript - 跳转到锚点 [英] JavaScript - Jump to anchor

查看:236
本文介绍了JavaScript - 跳转到锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图打开跳转到锚点的div。开场部分正在运作,但它没有跳到命名锚点

I'm trying to open a div with a jump to the anchor. The opening part is working but it's not jumping to the named anchor

这是我的剧本:

<script type="text/javascript">
    function spoil(id){
        if (document.getElementById) {
            var divid = document.getElementById(id);
            divid.style.display = (divid.style.display = 'block');
            window.location = '#' + id;
        }
    }
</script>

<a href="http://example.com" onclick="spoil('thanks');" title="hello">
    <img src="images/gfx.png" alt="world" width="300" height="300"/>
</a>

任何想法有什么问题吗?
干杯。

Any ideas what's wrong with it? Cheers.

推荐答案

看起来你正在取消隐藏扰流板div。如果是这样,您可以按如下方式将元素滚动到视图中:

Looks like you're unhiding a spoiler div. If so, you can scroll the element into view as follows:

function spoil(id) {
    var divid = document.getElementById(id);
    divid.style.display = 'block';
    divid.scrollIntoView(true);
    return false;
}
...
<a href="#" onclick="return spoil('thanks');" title="hello"><img src="images/gfx.png" alt="world" width="300" height="300"/></a>

这篇关于JavaScript - 跳转到锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆