Home > Web Front-end > JS Tutorial > Ajax implements clicking different links to display the returned content in a specific div

Ajax implements clicking different links to display the returned content in a specific div

亚连
Release: 2018-05-25 16:00:09
Original
2356 people have browsed it

Use ajax to click on different links on a web page, and then display the returned results in the fixed p of the page

/* The following code uses ajax to click on different links on a web page, The returned results are then displayed in the fixed p on the page. */

<html> 
<head> 
<meta charset="UTF-8"> 
<title>Insert title here</title> 
<script language="javascript"> 
var http_request = false; 
function createRequest(url,objID){ 
http_request = false; 
if(window.XMLHttpRequest){ //非IE浏览器 
http_request = new XMLHttpRequest(); 
if(http_request.overrideMimeType){ 
http_request.overrideMimeType("text/xml"); 
} 
}else if(window.ActiveXObject){ //IE浏览器 
try{ 
http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
}catch(e){ 
try{ 
http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
}catch(e){} 
} 
} 
if(!http_request){ 
alert("无法创建XMLHTTP实例"); 
return false; 
} 
http_request.open("GET",url,true); 
http_request.send(null); 

var obj = document.getElementById(objID); 
http_request.onreadystatechange = function(){ 
if(http_request.readyState == 4){ 
if(http_request.status == 200){ 
obj.innerHTML = http_request.responseText; 
}else{ 
alert(&#39;您请求的页面发现错误!&#39;); 
} 
} 
} 
} 
</script> 
</head> 

<body onload="createRequest(&#39;content1.html&#39;,&#39;show&#39;)"> 
<p align="center"> 
<a href="content1.html" onclick="createRequest(&#39;content1.html&#39;,&#39;show&#39;);return false;">no1</a> | 
<a href="content2.html" onclick="createRequest(&#39;content2.html&#39;,&#39;show&#39;);return false;">no2</a> | 
<a href="content3.html" onclick="createRequest(&#39;content3.html&#39;,&#39;show&#39;);return false;">no3</a> 
</p> 
<p id="show" align="center"></p> 
</body> 
</html>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax verification user name and password example code

Ajax implementation code for passing multiple parameters

laypage front-end paging plug-in implements ajax asynchronous paging

##

The above is the detailed content of Ajax implements clicking different links to display the returned content in a specific div. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template