Expanding a hidden div by referring with an anchor from external page

纵饮孤独 提交于 2019-12-02 06:52:23

You can do something like this on the target page:

window.onload = function() {
    var hash = window.location.hash; // would be "#div1" or something
    if(hash != "") {
        var id = hash.substr(1); // get rid of #
        document.getElementById(id).style.display = 'block';
    }
};

Essentially, you check on the page load whether the window's href has a hash attached to it. If it does, you find the <div> and change the style display to block.

You can use the window.location.hash to see the hash value. From there you can getElementById(hashValue) and show it.

Thank you! I figured out you can add this

location.href = '#' + id;

and also have the page scrolled to the position of the referred div.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!