JavaScript 未在地址栏中添加 id

标签 javascript jquery

出于某种原因,此 JavaScript 不会将单击的元素的 ID 添加到地址栏中。它将正确执行其他所有操作。

这是 jsfiddle: http://jsfiddle.net/4x7La64s/

例如,如果我点击#introduction,我希望它将ID放入地址栏中,如下所示:example.com/page.php/#introduction

最佳答案

基于您的代码,完成后:

e.preventDefault();

你可以这样做:

var hash = jQuery(this).attr("href"); //Your clicked link
if (window.history && window.history.pushState) {
  history.pushState(null, null, 'hash'); // This won't make the page jump to the id
} else {
  location.hash = hash; //This will be the fallback for old browsers and this will make the page jump to that id
}

关于JavaScript 未在地址栏中添加 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31302795/

相关文章:

javascript - Ace Editor - 只读变量

javascript - 使用 setTimeout() 函数对谷歌地图折线进行动画处理

javascript - 同位素等高行

javascript - 如何使用 jquery 验证插件验证字段

javascript - 如何将错误从 $.ajax 实例冒泡到 $.ajaxSetup?

jQuery 在回调函数中使用闭包来操作 $getJSON 数据

javascript - 使用 Jasmine 监视函数中的变量

javascript - 如何在 Google Apps 脚本 HTMLService 中设置链接? (错误与accounts.google.com的连接被取消)

javascript - 在模板文字中设置 HTML Button`onclick`

javascript - 使用 PHP 更改 CSS 不透明度