我有以下jQuery代碼,它在單擊后退按鈕時關(guān)閉引導模式。它工作正常。但是,當它關(guān)閉模式時,它應該從URL欄中刪除,它確實如此。但是,在這樣做的同時,它還會從URL中刪除其他“必需”參數(shù)。例如:#my-modal-id當 URL 為: 時,它可以正常工作。在這里,它根據(jù)腳本從URL欄中刪除。目前為止,一切都好。http://localhost/gamearena/index.php#my-modal-id#my-modal-id但是,當URL具有其他參數(shù)(例如)時,它會搞砸。在這里,它甚至會隨之刪除。我如何控制它,以便它只刪除之前自行設置的模態(tài)ID。http://localhost/gamearena/index.php?ref=shreyansh#sidebar-left?ref=shreyansh代碼如下:// Close model on clicking back button / swiping back $('div.modal').on('show.bs.modal', function() { var modal = this; var hash = modal.id; window.location.hash = hash; window.onhashchange = function() { if (!location.hash){ $(modal).modal('hide'); } } }); $('div.modal').on('hidden.bs.modal', function() { var hash = this.id; history.replaceState('', document.title, window.location.pathname); }); // when close button clicked simulate back $('div.modal button.close').on('click', function(){ window.history.back(); }) // when esc pressed when modal open simulate back $('div.modal').keyup(function(e) { if (e.keyCode == 27){ window.history.back(); } });編輯在診斷了一點之后,我發(fā)現(xiàn)這條線負責改變這里的URL的內(nèi)容,即刪除哈希值和其他參數(shù)。此外,我注意到抓取URL只到它上面的參數(shù),而不是超出它的參數(shù)。因此,在我看來,該函數(shù)將URL恢復為狀態(tài),而不是因為它無法識別它。因此,我得出的結(jié)論是,如果被一個能夠?qū)RL提取到(添加哈希部分之前的位置)的函數(shù)替換,那么問題將得到解決。因此,請專家對此進行一些闡述。history.replaceState('', document.title, window.location.pathname);window.location.pathnameindex.phpindex.phpindex.php?ref=shreyanshwindow.location.pathnameindex.php?ref=shreyansh
jQuery 函數(shù) history.replaceState() 從 URL 中刪除其他參數(shù)
MMTTMM
2022-09-02 21:19:09