Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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 如何在html中创建一个按钮链接,它将把您带到同一页面的不同部分_Javascript_Html_Css - Fatal编程技术网

Javascript 如何在html中创建一个按钮链接,它将把您带到同一页面的不同部分

Javascript 如何在html中创建一个按钮链接,它将把您带到同一页面的不同部分,javascript,html,css,Javascript,Html,Css,我想做一个按钮,当你点击它时,它会把你带到同一页面的不同部分,但不是不同的页面。按钮将带您进入同一页面的特定部分,而不是向下滚动页面。例如,在您的主页上,有一个关于我们的部分,您希望访问者通过单击按钮/链接进入关于我们的部分。您可以在“关于我们”部分中设置id。像这样: <div id="about">This is About Us Section<div> 也许您正在寻找scrollTo()方法 使用JavaScript滚动到指定的坐标: fun

我想做一个按钮,当你点击它时,它会把你带到同一页面的不同部分,但不是不同的页面。按钮将带您进入同一页面的特定部分,而不是向下滚动页面。

例如,在您的主页上,有一个关于我们的部分,您希望访问者通过单击按钮/链接进入关于我们的部分。您可以在“关于我们”部分中设置id。像这样:

<div id="about">This is About Us Section<div>

也许您正在寻找scrollTo()方法

使用JavaScript滚动到指定的坐标:

function scrollWin() {

  window.scrollTo(h{int}, y{int});
}
按钮看起来像:

<button onclick="scrollWin()">Scroll to {Section}</button>
滚动到{Section}

如果您想在编写时使用Button not Link,那么可以将带有url的表单元素中的Button添加到同一页面和锚定中,如下面的代码示例所示

<html>
    <head>
        <title>Page</title>
    </head>
    <body>

        <form method="get" action="#anchor">
            <button type="submit">Button</button>
        </form>

        <div style="height:900px;"></div>
        <p id="anchor">Paragraph 1</p>
        <div style="height:900px;"></div>
        
    </body>
</html>

页
按钮

第1段


实际上有很多关于这个主题的博客文章,在下次发布问题之前尝试搜索。
<button onclick="scrollWin()">Scroll to {Section}</button>
<html>
    <head>
        <title>Page</title>
    </head>
    <body>

        <form method="get" action="#anchor">
            <button type="submit">Button</button>
        </form>

        <div style="height:900px;"></div>
        <p id="anchor">Paragraph 1</p>
        <div style="height:900px;"></div>
        
    </body>
</html>