jQuery and ajax implement partial refresh
This article mainly shares with you two different methods of jQuery and ajax to achieve partial refresh. In projects, ajax is often used, such as to achieve partial refresh, such as front-end and back-end interaction, etc. Here we share two methods of partial refresh The method mainly uses .load() in ajax. Friends who are interested should take a look together.
First type:
When several pages have the same header, navigation, and bottom, click the navigation link to switch between several pages. At this time, you want The effect is that after clicking the link, only the content part is switched, and the other parts 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, and are only displayed in the p of #container The content 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. If there is too much content on the right side, it is not suitable for tabs. This It is best to use .load() to refresh locally. 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. Reload a certain part of the page through .load(). Please note that 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 attributes of data-* to store data. When clicking, the anchor point is modified. Because the address has changed, the current page content will still be maintained when refreshing instead of switching to First.
Related recommendations:
Ajax implements partial refresh login interface with verification code
JQuery ajax partial refresh example
Partial refresh verification code implemented by jsp+ajax (onblur event triggers verification)
The above is the detailed content of jQuery and ajax implement partial refresh. 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

Is the F5 key not working properly on your Windows 11/10 PC? The F5 key is typically used to refresh the desktop or explorer or reload a web page. However, some of our readers have reported that the F5 key is refreshing their computers and not working properly. How to enable F5 refresh in Windows 11? To refresh your Windows PC, just press the F5 key. On some laptops or desktops, you may need to press the Fn+F5 key combination to complete the refresh operation. Why doesn't F5 refresh work? If pressing the F5 key fails to refresh your computer or you are experiencing issues on Windows 11/10, it may be due to the function keys being locked. Other potential causes include the keyboard or F5 key

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 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

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:

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: <

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:

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.

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
