How to avoid anchors in iframes to scroll parent page

后端 未结 6 2330
一向
一向 2021-02-13 03:54

So, here\'s the problem: I have a quite long page with an iframe in the middle. Now, if an anchor is clicked in the iframe, the entire page is scrolled to the iframe, which is w

6条回答
  •  独厮守ぢ
    2021-02-13 03:58

    Maybe it's a bug in chrome, because this issue not happen in the latest IE and Firefox.

    It happended when clicked anchor in iframe, and browser try to align the anchor to the top of the window.

    I solved this issue use JavaScript(jQuery):

    $('body').on('click', 'a', function(e) {
        if($(this).attr('href').startsWith('#')) {
            e.preventDefault();
        }
    });
    

    I hope it can help you & good luck!

提交回复
热议问题