jquery - 如何更改 url 以保持当前 HTML
问题描述
通过单击按钮,URL 必须更改,但 html 应保持不变
$('#v-pills-profile-tab').on('click', function (event) {
// change url
// and html remains
})
解决方案
您可以使用History.pushState()和WindowEventHandlers.onpopstate
<button id="v-pills-profile-tab"> TEST ME </button>
<script>
$('#v-pills-profile-tab').on('click', function (event) {
window.history.pushState({data: "some data that i will need"}, "", "my-new-url");
});
window.onpopstate = function (e) {
if (e.state) {
console.log(e.state.data);
} else {
window.location.reload();
return;
}
};
</script>
推荐阅读
- codeigniter - 无法加载您的默认控制器 请确保您的 Routes.php 文件中指定的控制器有效
- javascript - 选择声明为变量的多个元素
- javascript - javascript中用于添加操作的数组计算
- .net - 如何在一周中的特定日期运行hangfire
- fluentd - fluentd 遇到解析器错误时的行为。解析器死信队列
- javascript - 为什么我在位置 0 的 JSON 中收到 Unexpected token < 的错误
- ios - UILabel 应用变换并设置布局约束
- python - 无法解析 url Django 中的日期
- git - 如何更新镜像的 git 存储库
- ios - 如何将 .ics 文件发布到我的 iPhone 日历中?