我正在實際創建一個 reloadPage 函式,該函式需要和 Id,然后重新加載(加載)它。由于加載頁面后我無法使用任何功能,所以我基本上想在其中執行一個 init 功能。
所以這里是我的功能
function reloadPage(id) {
$("#" id).load(window.location " #" id);
}
我想做一個初始化函式,所以我可以呼叫它。
就像
reloadPage(test, init())
這可能嗎?我將如何處理這樣的問題。
uj5u.com熱心網友回復:
有很多方法可以將資料從前一個實體傳遞到頁面,其中之一是WebStorage api
因此,為了在頁面加載時運行特定的代碼塊,您可以使用 sessionStorage 存盤您想要的任何標志,然后檢查該標志是否存在并運行您想要的功能,它可能不是您想要的,但因為您沒有提供一個可重復的示例(我如何提出一個好問題?),我希望它為您提供如何在您的專案中實作此類功能的線索和方向
例如(將其保存為 html,加載并檢查您的控制臺):
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>
<body>
<input>
<button>TEST</button>
<script>
$(document).on('click', 'button', function () {
let func = $("input").val() //give your desired function here
sessionStorage.setItem("function", func); //store whatever you typed in the session storage
location.reload(); //now reload the page
})
window.onload = function () { //this function runs code after a page completely loads
$("input").val("function 1") //just for the example i set an initial value to the input
let funct = sessionStorage.getItem("function"); // get the data you stored previously
if( funct === "function 1" ){ // check for specific flags
console.log("This is function 1")
}
if( funct === "function 2" ){
console.log("This is function 2")
}
};
</script>
</body>
</html>
您還可以了解 Url 引數并將查詢字串傳遞給您要傳遞的值的重新加載 url。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/407058.html
標籤:
