<\/span>\r\n<\/p>\r\n<\/body>\r\n<\/html><\/pre> <\/p>
<\/p>
Configuration server: Server.php<\/p>
<\/p>
<\/p>
ajaxDemo.js implements AJax asynchronous access: <\/p>
$(document).ready(function(){\r\n $(\"#btn\").on(\"click\",function(){\r\n \/\/在與服務(wù)器通訊較慢時給用戶提示信息\r\n $(\"#result\").text(\"數(shù)據(jù)請求中,請稍后...\");\r\n \/\/向服務(wù)器發(fā)送請求(get、post)\r\n $.get(\"Server.php\",{name:$(\"#nameValue\").val()},function(data){\r\n $(\"#result\").text(data);\r\n }).error(function(){\r\n \/\/當(dāng)服務(wù)器出現(xiàn)異常時\r\n $(\"#result\").text(\"服務(wù)器正在維護(hù)\")\r\n })\r\n })\r\n})<\/pre> <\/p>
<\/p>
Achieve the effect: <\/p>
<\/p>
【Asynchronous Loading】
<\/p>
Mainly use the load() method and getScript() method, specifically explained with an example:
<\/p>
Load a prepared fragment in the existing html file, and in the fragment A popup that prevents the user from further action until loading is complete.
<\/p>
The first is the existing html code without any content:
<\/p>
\r\n \r\n \r\n \r\n AJax異步加載<\/title> \r\n
Home
php教程
PHP開發(fā)
Use jQuery's AJax to implement asynchronous access and asynchronous loading
Use jQuery's AJax to implement asynchronous access and asynchronous loading
Dec 08, 2016 am 11:15 AM
jquery
This article implements asynchronous access and asynchronous loading using jQuery's AJax. It has certain reference value. Interested friends can refer to it.
【Asynchronous Access】
Use an example to illustrate: click the button, send the data entered by the user in the input to the server, and return the result to the page.
The first is the html content:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>AJax異步訪問</title>
<script src="js/jquery-2.1.3.min.js"></script>
<script src="js/ajaxDemo.js"></script>
</head>
<body>
<input type="text" id="nameValue">
<br/>
<button id="btn">提交</button>
<p>
結(jié)果:<span id="result"></span>
</p>
</body>
</html>
Configuration server: Server.php
<?php
if(isset($_GET['name'])){
echo "姓名:".$_GET['name'];
}else{
echo "參數(shù)錯誤";
}
ajaxDemo.js implements AJax asynchronous access:
$(document).ready(function(){
$("#btn").on("click",function(){
//在與服務(wù)器通訊較慢時給用戶提示信息
$("#result").text("數(shù)據(jù)請求中,請稍后...");
//向服務(wù)器發(fā)送請求(get、post)
$.get("Server.php",{name:$("#nameValue").val()},function(data){
$("#result").text(data);
}).error(function(){
//當(dāng)服務(wù)器出現(xiàn)異常時
$("#result").text("服務(wù)器正在維護(hù)")
})
})
})
Achieve the effect:

【Asynchronous Loading】
Mainly use the load() method and getScript() method, specifically explained with an example:
Load a prepared fragment in the existing html file, and in the fragment A popup that prevents the user from further action until loading is complete.
The first is the existing html code without any content:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AJax異步加載</title>
<script src="js/jquery-2.1.3.min.js"></script>
<script src="js/main.js"></script>
</head>
<body>
</body>
</html>
Draw a js file getData.js and write a simple pop-up box prompt for the function as an example:
function getData(){
alert("片段的內(nèi)容引自新浪體育");
}
擬一個片段box.htm,承載要加載的片段內(nèi)容:
<div>
<h4>中超-耿曉峰失誤拉蒙兩球 申花7輪首敗1-4綠城</h4>
<p>
北京時間8月11日晚19點35分,2015年中超聯(lián)賽第22輪在杭州黃龍體育場開始一場較量,
由杭州綠城迎戰(zhàn)上海申花。上半場第7分鐘陳柏良突然冷射,導(dǎo)致耿曉峰接球脫手造成失球,
這是中華臺北球員(陳昌源在比利時土生土長)在中超聯(lián)賽進(jìn)的首球。
第12分鐘申花隊呂征禁區(qū)右路左腳凌空射門扳平比分。第25分鐘阿甘在底線附近把球送入禁區(qū),
拉蒙頭球得分,第37分鐘阿甘單刀赴會打入一球。
第60分鐘阿甘頭球擺渡,拉蒙跟進(jìn)射門梅開二度。最終杭州綠城4比1戰(zhàn)勝上海申花。
</p>
</div>
Finally write main.js to load getData asynchronously. js and box.htm into the existing html file.
$(document).ready(function(){
//異步加載js文件
$.getScript("js/getData.js").complete(function(){
getData();
})
//異步加載片段
$("body").text("加載中...")
$("body").load("box.htm",function(url,status,c){
if(status=="error"){
$(this).text("片段加載失敗");
}
});
})
Final effect:

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
Detailed explanation of jQuery reference methods: Quick start guide
Feb 27, 2024 pm 06:45 PM
Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded
How to use PUT request method in jQuery?
Feb 28, 2024 pm 03:12 PM
How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u
In-depth analysis: jQuery's advantages and disadvantages
Feb 27, 2024 pm 05:18 PM
jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,
jQuery Tips: Quickly modify the text of all a tags on the page
Feb 28, 2024 pm 09:06 PM
Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <
Use jQuery to modify the text content of all a tags
Feb 28, 2024 pm 05:42 PM
Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ??the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:
How to remove the height attribute of an element with jQuery?
Feb 28, 2024 am 08:39 AM
How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element
Understand the role and application scenarios of eq in jQuery
Feb 28, 2024 pm 01:15 PM
jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
Introduction to how to add new rows to a table using jQuery
Feb 29, 2024 am 08:12 AM
jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
See all articles