Home > Web Front-end > JS Tutorial > body text

How to implement partial refresh function with jQuery+ and ajax

php中世界最好的语言
Release: 2018-04-25 15:07:01
Original
1591 people have browsed it

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>
Copy after login

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
});
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. 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>
Copy after login

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);
});
Copy after login

user_center.html:

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!