1 回答

TA貢獻1851條經(jīng)驗 獲得超5個贊
使用XMLHttpRequest,例如:
function World(){
const xhr = new XMLHttpRequest;
xhr.open('POST', 'hello_world.txt'); // change url accordingly
xhr.onload = resp=>{
document.getElementById('test').textContent = resp.responseText;
}
xhr.send();
}
就我個人而言,我會嘗試將 HTML 與 JavaScript 分開。onload='World()所以,如果我們在你的標簽中去掉了<body>,并且你像你的test.js頁面一樣使用外部 JavaScript,它會像:
let doc, bod, I; // for use on other loads
addEventListener('load', ()=>{ // same as your body load Event - everything deeper than load is indented
doc = docmuent; bod = doc.body; I = id=>doc.getElementById(id);
const xhr = new XMLHttpRequest;
xhr.open('POST', 'hello_world.txt'); // change url accordingly
xhr.onload = resp=>{
I('test').textContent = resp.responseText;
}
xhr.send();
}); // end load
- 1 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報