假設你使用jQuery,下面的代碼應該足夠了。
$("button").click(function(){// any event you can bind on which you want to load new page.
$("html").load("usa", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
下面是完整的示例HTML代碼以上說明。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("html").load("usa", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
使用便接踵而來jQuery腳本:'$( 「HTML」)負載(NEWURL)' – diavolic
聽說過路由? –
@diavolic你可以幫助一個詳細的代碼答案。我對JavaScript一無所知。 :( – bobozar