Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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
html/javascript-跳转到锚点不工作_Javascript_Html - Fatal编程技术网

html/javascript-跳转到锚点不工作

html/javascript-跳转到锚点不工作,javascript,html,Javascript,Html,当我的按钮被点击时,我有一个指向锚的onClick函数。当您单击按钮时,您可以看到#出现在url中,页面快速跳转到定位点,但随后url中的#消失,页面跳回顶部。我想知道是否有人知道是什么导致了这个问题,以及如何修复它,使页面保持在锚定位置 <button type="submit" class="btn btn-primary" name='search' onClick="location.href='index.php#results'">Search</button>

当我的按钮被点击时,我有一个指向锚的onClick函数。当您单击按钮时,您可以看到#出现在url中,页面快速跳转到定位点,但随后url中的#消失,页面跳回顶部。我想知道是否有人知道是什么导致了这个问题,以及如何修复它,使页面保持在锚定位置

<button type="submit" class="btn btn-primary" name='search' onClick="location.href='index.php#results'">Search</button>
搜索
然后在后面的页面中

<a id="results"></a> 

您需要使用
您已将单击事件侦听器附加到提交按钮

据推测,它在一个表单中

单击它会触发事件,运行JS,然后提交表单(因为这是提交按钮的用途)

如果要链接到页面的另一部分,请使用链接。如果你想让它看起来像一个按钮,可以使用CSS

<a href="#results" class="btn btn-primary">Scroll to results</a>


如果您的目标是:

  • 提交表格
  • 加载带有结果的页面
  • 然后滚动到锚点

  • 然后查看。

    这是无效的HTML。链接可能不会出现在按钮内。(在按钮中添加链接是没有意义的)。
    <a href="#results" class="btn btn-primary">Scroll to results</a>