Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript方法移动到另一个页面,然后移动到页面的某个部分_Javascript_Html - Fatal编程技术网

如何使用Javascript方法移动到另一个页面,然后移动到页面的某个部分

如何使用Javascript方法移动到另一个页面,然后移动到页面的某个部分,javascript,html,Javascript,Html,我有3个文件: mainPage.html starter.html Landing.js 当我单击starter.html中的div时,我想转到mainPage.html,然后转到该页面的菜单部分 starter.html代码: <li> <div onclick="onBackToMenu()">Back to menu</div></li> function onBackToMenu() { window.lo

我有3个文件:

  • mainPage.html
  • starter.html
  • Landing.js
当我单击starter.html中的div时,我想转到mainPage.html,然后转到该页面的菜单部分

starter.html代码:

<li> <div onclick="onBackToMenu()">Back to menu</div></li>
function onBackToMenu() {
   window.location.href = "../mainPage.html";

   document.getElementById("Menu").scrollIntoView();

}
mainPage.html:(部分代码)


菜单/产品
起动装置
当我点击div时,它确实会带我去mainPage.html,但它不想带我去id为Menu的菜单部分


请帮助

,因为您已经在该部分设置了id==>
在href末尾传递id,如下所示:
window.location.href=“../mainPage.html#Menu”这将在加载新页面时引导您到该部分

函数onBackToMenu(){
window.location.href=“../mainPage.html#菜单”;
document.getElementById(“菜单”).scrollIntoView();
}

因为您在该部分已经有了id==>
在href末尾传递id,如下所示:
window.location.href=“../mainPage.html#Menu”这将在加载新页面时引导您到该部分

函数onBackToMenu(){
window.location.href=“../mainPage.html#菜单”;
document.getElementById(“菜单”).scrollIntoView();
}

不需要javascript。更好的做法是使用带有
href
元素的创建指向新页面的超链接,并为要滚动到的部分创建一个带有的

在您的情况下,只需将starter.html中的列表项更改为:


  • 这将引导用户进入新页面的菜单部分。

    不需要javascript。更好的做法是使用带有
    href
    元素的创建指向新页面的超链接,并为要滚动到的部分创建一个带有的

    在您的情况下,只需将starter.html中的列表项更改为:


  • 这将引导用户进入新页面的菜单部分。

    谢谢,我不知道您可以在页面后设置标签。那太容易了!谢谢,我不知道你可以在页面后设置标签。那太容易了!如果您的问题已解决,请将答案标记为已接受。如果您的问题已解决,请将答案标记为已接受。
    <!--    Menu section -->
        <span class="anchor-offset" id="Menu"></span>
        <div class ="section" >
            
            <div class ="sectionHeadings">
                <h1>Menu/Products</h1>
                <div class="menu">
                    
                    <div class="starter" onclick="location.href='pages/starter.html';">
                        <h3 class="menu-heading starter-heading">Starters</h3>
                    </div>