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!