本文主要是介绍Ajax起步,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Ajax的关键在于XMLHttpRequest对象,理解这个对象的例子如下:
<!DOCTYPE HTML>
<html>
<head>
<title>Ajax</title>
</head>
<body>
<div>
<button>Apple</button>
<button>Samsung</button>
<button>IBM</button>
</div>
<div id="target">
Press a button
</div>
<script>
var buttons = document.getElementsByTagName("button");
for(var i = 0; i < buttons.length; i++){
buttons[i].onclick = handleButtonPress;
}
function handleButtonPress(e){
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = handleResponse;
httpRequest.open("GET", e.target.innerHTML + ".html");
httpRequest.send();
}
function handleResponse(e){
if(e.target.readyState == XMLHttpRequest.DONE &&
e.target.status == 200){
document.getElementById("target").innerHTML
= e.target.responseText;
}
}
</script>
</body>
</html>
这篇关于Ajax起步的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!