链接可以滚动到页面顶部,也可以在iframe中打开页面

Can a link scroll to the top of a page as well as open a page in an iframe?

本文关键字:iframe 也可以 顶部 滚动 链接      更新时间:2023-09-26

我想弄清楚我如何能得到我所有的链接滚动到页面的顶部,以及在我的iframe打开所需的页面。这可能吗?我需要这个链接来执行两个不同的功能。

警报标准

                <script>
                    function myFunction() {
                    // Scroll to top
                                document.body.scrollTop = document.documentElement.scrollTop = 0;
                    // Open up a link in my iframe
                            <A HREF="DocDisplayCategory.cfm?CategoryID=47" TARGET="MainWindow">
                            </A>
                                  } 
                </li>
                </script>

你可以直接使用事件处理程序。

<a href="#" onClick="myFunction();" >Click Me</a>
<script>
    function myFunction() {
        // Scroll to top
        document.body.scrollTop = document.documentElement.scrollTop = 0;
        // Open up a link in my iframe
        document.getElementById('frame1').src="http://www.foo.com/" 
    }
</script>

关于滚动,请看这里;滚动到使用JavaScript/jQuery页面的顶部?
要在iframe中打开链接,只需指定target

你也可以使用jquery自动创建目标