How to implement partial refresh function with jQuery+ and ajax
This time I will show you how jQuery and ajax implement the partial refresh function. What are the precautions for jQuery and ajax to implement the partial refresh function. The following is a practical case, let's take a look.
When certain pages have the same header, Navigation, and bottom, clicking the navigation link can switch between several pages. The desired effect at this time is to click the link Afterwards, only the content part is switched, and the others are not reloaded. Up the code.
jq-load.html:
<!DOCTYPE html> <html> <head> <title>ajax局部刷新</title> </head> <body> <header> <nav> <a href="jq-load.html" rel="external nofollow" class="current">首页</a> <a href="jq-load2.html" rel="external nofollow" >新闻资讯</a> <a href="jq-load3.html" rel="external nofollow" >用户中心</a> </nav> </header> <section id="content"> <p id="container"> 首页的内容 </p> </section> <script src="js/jquery-1.11.0.min.js"></script> <script src="js/jq-load.js"></script> </body> </html>
Note: The codes of jq-load2.html, jq-load3.html and jq-load.html are basically the same, only in The content displayed in the p of #container is different.
jq-load.js:
$('nav a').on('click', function(e) { e.preventDefault(); // 阻止链接跳转 var url = this.href; // 保存点击的地址 $('nav a.current').removeClass('current'); $(this).addClass('current'); $('#container').remove(); $('#content').load(url + ' #container').fadeIn('slow'); // 加载新内容,url地址与该地址下的选择器之间要有空格,表示该url下的#container });
Note: This method uses some new tags in html5. Creating them in js will not be described again.
Second type:
If there is a list on the left side of the web page, click the list to switch the content on the right side. There is too much content and is not suitable for tabs. At this time, it is best to use .load() to partially refresh. Up the code.
user.html:
<!DOCTYPE html> <html lang="en"> <head> <title>个人中心</title> <meta charset="utf-8"> <script src="js/jquery-1.11.0.min.js"></script> <script src="js/user.js"></script> </head> <body> <p class="userWrap"> <ul class="userMenu"> <li class="current" data-id="center">用户中心</li> <li data-id="account">账户信息</li> <li data-id="trade">交易记录</li> <li data-id="info">消息中心</li> </ul> <p id="content"></p> </p> </body> </html>
user.js:
$(function(){ $(".userMenu").on("click", "li", function(){ var sId = $(this).data("id"); //获取data-id的值 window.location.hash = sId; //设置锚点 loadInner(sId); }); function loadInner(sId){ var sId = window.location.hash; var pathn, i; switch(sId){ case "#center": pathn = "user_center.html"; i = 0; break; case "#account": pathn = "user_account.html"; i = 1; break; case "#trade": pathn = "user_trade.html"; i = 2; break; case "#info": pathn = "user_info.html"; i = 3; break; default: pathn = "user_center.html"; i = 0; break; } $("#content").load(pathn); //加载相对应的内容 $(".userMenu li").eq(i).addClass("current").siblings().removeClass("current"); //当前列表高亮 } var sId = window.location.hash; loadInner(sId); });
user_center.html:
<p> 用户中心 …… </p>
Note: Other user_xxx.html pages are also listed. The corresponding content will not be repeated here.
Summary:
The principles of the above two methods are the same. You need to pay attention to reloading a certain part of the page through .load(). , ajax needs to run in a server environment. Through comparison, it can be found that the first one is relatively simple, and the second one is slightly more complicated. However, I personally recommend the second one. The first one is mainly to give an example to see how .load() is used. In fact, it plays an important role in user experience. It is slightly inferior in aspects. For example, when clicking, the address in the address bar does not change, making forward and backward invalid. This can be implemented later. The second method is more flexible in application. It cleverly uses the custom attribute of data-* to store data. When clicking, the anchor point is modified. Because the address has changed, the current value will still be maintained when refreshing. page content instead of switching to the first one.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of Ajax user authentication and registration
How to implement ajax loading timeout prompt
Display progress during Ajax request
The above is the detailed content of How to implement partial refresh function with jQuery+ and ajax. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.

Ajax is not a specific version, but a technology that uses a collection of technologies to asynchronously load and update web page content. Ajax does not have a specific version number, but there are some variations or extensions of ajax: 1. jQuery AJAX; 2. Axios; 3. Fetch API; 4. JSONP; 5. XMLHttpRequest Level 2; 6. WebSockets; 7. Server-Sent Events; 8, GraphQL, etc.
