首页 > 解决方案 > 如何更改 url 以保持当前 HTML

问题描述

通过单击按钮,URL 必须更改,但 html 应保持不变

$('#v-pills-profile-tab').on('click', function (event) {
   // change url
   // and html remains
})

标签: jqueryajax

解决方案


您可以使用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>

推荐阅读