2 回答

TA貢獻1802條經驗 獲得超5個贊
這是一個使用 vanilla JavaScript 的簡單解決方案:
Page01.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>Page 1</div>
<a href="page02.html">go to page 2</a>
<script>
var linkToPage2 = document.querySelector("a[href='page02.html']");
linkToPage2.addEventListener("click", function(e){
e.preventDefault();
if(location.search){
window.location.href = "page02.html" + location.search;
}
});
</script>
</body>
</html>
Page02.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>Page 2</div>
<a href="page01.html">go to page 1</a>
<script>
var linkToPage1 = document.querySelector("a[href='page01.html']");
linkToPage1.addEventListener("click", function(e){
e.preventDefault();
if(location.search){
window.location.href = "page01.html" + location.search;
}
});
</script>
</body>
</html>

TA貢獻1795條經驗 獲得超7個贊
我同時使用了 Bergi 的評論和 Henry 的回答來創建我自己的版本。
基本上我只是在a 標簽上創建了一個onclick="menuClick();"和一個id="menuLink"。然后在 Javascript 上,我剛剛添加location.search了 href:
menuClick = function() {
document.getElementById('menuLink').href += location.search
}
添加回答
舉報