window.onload = function() {var oBtn = document.getElementById('btn');oBtn.onclick = function() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if ( xhr.status == 200 ) { alert( xhr.responseText ); } } }//教程中說把監聽的事件函數寫在xhr.open('get', '1.txt', true);xhr.send();前面比較好,為什么呢? xhr.open('get', '1.txt', true); xhr.send(); }}下面這么寫也沒處問題額?window.onload = function() {var oBtn = document.getElementById('btn');oBtn.onclick = function() { //打開瀏覽器 var xhr = new XMLHttpRequest(); //在地址欄輸入地址 xhr.open('get','1.txt',true); //提交 xhr.send(); //等待服務器返回內容 xhr.onreadystatechange = function() { if ( xhr.readyState == 4 ) { alert( xhr.responseText ); } } }}
關于Ajax寫的順序的好壞
函數式編程
2019-03-01 18:22:37