[英]Get HTML From Another File Using Javascript
我有一組按鈕,當我單擊它們時,我試圖顯示來自不同文件的 HTML。 我嘗試涉足 JQuery,但我決定嘗試使用 fetch
如果您使用 jquery,您可以嘗試使用.load()方法:
$(content).load('content.php');
或者,如果您仍想使用fetch
方法:
fetch('content.php')
.then(function(response) {
var html = response.text();
var parser = new DOMParser();
var doc = parser.parseFromString(html, "text/html");
content.innerHTML = doc.innerHTML;
});
這是使用fetch()
執行此操作的正確方法。
fetch('content.php').then(res => {
if (response.status !== 200) {
console.log(`Looks like there was a problem. Status Code: ${response.status}`);
return
}
res.text().then(text => {
content.innerHTML = text
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.