Home > Web Front-end > JS Tutorial > jQuery+ajax implements partial refresh

jQuery+ajax implements partial refresh

巴扎黑
Release: 2017-06-26 14:34:00
Original
1477 people have browsed it

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, mainly using .load() in ajax.

The first type:

When several pages have the same header, navigation, and bottom, you can click on the navigation link to switch between several pages. Switching in the page, the desired effect at this time is to only switch the content part after clicking the link, and the other parts will not be reloaded. Up the code.

jq-load.html:

 1 <!DOCTYPE html> 2 <html> 3   <head> 4     <title>ajax局部刷新</title> 5   </head> 6   <body> 7  8     <header> 9       <nav>10         <a href="jq-load.html" class="current">首页</a>11         <a href="jq-load2.html">新闻资讯</a>12         <a href="jq-load3.html">用户中心</a>13       </nav>14     </header>15 16     <section id="content">17       <div id="container">18           首页的内容19       </div>20     </section>21 22     <script src="js/jquery-1.11.0.min.js?1.1.11"></script>23     <script src="js/jq-load.js?1.1.11"></script>24 25   </body>26 </html>
Copy after login

Note: jq-load2.html, jq-load3.html and jq- The load.html code is basically the same, only the content displayed in the #container div is different.

jq-load.js:

 1 $('nav a').on('click', function(e) {                 
 2   e.preventDefault();  // 阻止链接跳转 3   var url = this.href;  // 保存点击的地址 4  5   $('nav a.current').removeClass('current');    
 6   $(this).addClass('current');                       
 7  8   $('#container').remove();                          
 9   $('#content').load(url + ' #container').fadeIn('slow'); // 加载新内容,url地址与该地址下的选择器之间要有空格,表示该url下的#container10 });
Copy after login

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 the content on the right side is too There are too many, so it is not suitable for tabs. At this time, it is best to use .load() to partially refresh. Up the code.

user.html:

 1 <!DOCTYPE html> 2 <html lang="en"> 3     <head> 4         <title>个人中心</title> 5         <meta charset="utf-8"> 6         <script src="js/jquery-1.11.0.min.js?1.1.11"></script> 7         <script src="js/user.js?1.1.11"></script> 8     </head> 9     <body>10 11         <div class="userWrap">17             <ul class="userMenu">18                 <li class="current" data-id="center">用户中心</li>19                 <li data-id="account">账户信息</li>20                 <li data-id="trade">交易记录</li>21                 <li data-id="info">消息中心</li>22             </ul>23             <div id="content"></div>25         </div>26         27     </body>28 </html>
Copy after login

user.js:

$(".userMenu").on("click", "li",  sId = $().data("id");  window.location.hash = sId;   sId = "#center": pathn = "user_center.html"; i = 0;  "#account": pathn = "user_account.html"; i = 1;  "#trade": pathn = "user_trade.html"; i = 2;  "#info": pathn = "user_info.html"; i = 3; : pathn = "user_center.html"; i = 0; "#content").load(pathn); $(".userMenu li").eq(i).addClass("current").siblings().removeClass("current");  sId =
Copy after login

user_center.html:

<div>用户中心
    ……</div>
Copy after login

Note: Other user_xxx.html pages also correspond to the list and 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 be in the server environment. run below. 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.


The above is the detailed content of jQuery+ajax implements partial refresh. 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