<!DOCTYPE html>
<html>
<head>
<title>Second</title>
<script type="text/javascript" src = "https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript">
var xhr;
function createXmalHRequest(){
if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
return new XMLHttpRequest;
}
}
function handleStateChange(){
if(xhr.readyState == 4){
if(xhr.status == 200){
alert("the change is"+xhr.responseText);
$(document).ready(function(){
$("p.a").html(xhr.responseText);
})
}
}
}
function startAjax(){
xhr = createXmalHRequest();
xhr.open("GET","gagaga.txt",true);
xhr.onreadystatechange = handleStateChange;
xhr.send(null);
}
$(function(){
$("p.b").click(function(){testAjax()});
});
function testAjax(){
var xhr1 = new XMLHttpRequest();
console.log(xhr1.readyState);
xhr1.open("GET","abc.txt",true);
xhr1.onload = function(){
$("p.b").html(xhr1.responseText);
}
xhr1.send(null);
};
</script>
</head>
<body>
<input type="button" name="btn1" value="onClick" onclick="startAjax()">
<p class="a">ddfddddd</p>
<p class="b">Hello Pb</p>
</body>
</html>
$(function(){
$("p.b").click(testAjax());
});
function testAjax(){
var xhr1 = new XMLHttpRequest();
console.log(xhr1.readyState);
xhr1.open("GET","abc.txt",true);
xhr1.onload = function(){
$("p.b").html(xhr1.responseText);
}
xhr1.send(null);
};
这样写不对
$(function(){
$("p.b").click(function(){testAjax()});
});
这样写才可以。