1 回答
TA贡献1851条经验 获得超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 关注
- 81 浏览
添加回答
举报