Home Web Front-end JS Tutorial Ajax+div page layout effect

Ajax+div page layout effect

Apr 04, 2018 am 09:36 AM
Effect page

This time I will bring you the effect of Ajax+divpage layout, what are the notes of Ajax+div page layout, the following is a practical case, let's take a look.

Effect demonstration:

①Default page (index.jsp):

②: Click on the left User Management# After selecting the user list option under the ## tag, the content of the default page on the right is updated to the content of the user list page (userList.jsp):

③: Similarly, click Product Management , the product list and order list under the order management tag, the content on the right side will be refreshed accordingly to the content of the product list page (productList.jsp) and order list page (recordList.jsp)

In this way, Ajax is used +p realizes the effect of menu selection on the left and content display on the right. Let’s take a look at the specific implementation process

1:

Sample program for the entire demonstration Contains the default page (index.jsp), user list page (userList.jsp), product list page (productList.jsp), order management page (recordList.jsp)

Uses the Bootstrap framework and FontAwesome icon ( No way, bootstrap comes with too few icons o(╯□╰)o). Please refer to the official website for how to use these two products. They are relatively simple.

Let’s take a look at the code of index.jsp first. I basically commented in detail:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">  <%-- 在IE运行最新的渲染模式 --%>
    <meta name="viewport" content="width=device-width, initial-scale=1">  <%-- 初始化移动浏览显示 --%>
    <meta name="Author" content="Dreamer-1.">
    <!-- 引入各种CSS样式表 -->
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/font-awesome.css">
    <link rel="stylesheet" href="css/index.css">  <!-- 修改自Bootstrap官方Demon,你可以按自己的喜好制定CSS样式 -->
    <link rel="stylesheet" href="css/font-change.css">  <!-- 将默认字体从宋体换成微软雅黑(个人比较喜欢微软雅黑,移动端和桌面端显示效果比较接近) -->    
    <script type="text/javascript" src="js/jquery-1.12.3.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <title>- 后台管理系统 -</title>
  </head>
  <body>
  <!-- 顶部菜单(来自bootstrap官方Demon)==================================== -->
    <nav class="navbar navbar-inverse navbar-fixed-top">
       <p class="container">
        <p class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" >
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
           <a class="navbar-brand" href="index.jsp">XXXX.com</a>
        </p>
        <p id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">            
            <li><a href="###" onclick="showAtRight(&#39;userList.jsp&#39;)"><i class="fa fa-users"></i> 用户列表</a></li>  
            <li><a href="###" onclick="showAtRight(&#39;productList.jsp&#39;)"><i class="fa fa-list-alt"></i> 产品列表</a></li>
            <li><a href="###" onclick="showAtRight(&#39;recordList.jsp&#39;)" ><i class="fa fa-list"></i> 订单列表</a></li>  
          </ul>
        </p>
       </p>
    </nav>
  <!-- 左侧菜单选项========================================= -->
    <p class="container-fluid">
      <p class="row-fluie">
        <p class="col-sm-3 col-md-2 sidebar">    
          <ul class="nav nav-sidebar">
            <!-- 一级菜单 -->
            <li class="active"><a href="#userMeun" class="nav-header menu-first collapsed" data-toggle="collapse">
              <i class="fa fa-user"></i>  用户管理 <span class="sr-only">(current)</span></a>
            </li> 
            <!-- 二级菜单 -->
            <!-- 注意一级菜单中<a>标签内的href="#……"里面的内容要与二级菜单中<ul>标签内的id="……"里面的内容一致 -->
            <ul id="userMeun" class="nav nav-list collapse menu-second">
              <li><a href="###" onclick="showAtRight(&#39;userList.jsp&#39;)"><i class="fa fa-users"></i> 用户列表</a></li>
            </ul>
            <li><a href="#productMeun" class="nav-header menu-first collapsed" data-toggle="collapse">
              <i class="fa fa-globe"></i>  产品管理 <span class="sr-only">(current)</span></a>
            </li> 
            <ul id="productMeun" class="nav nav-list collapse menu-second">
              <li><a href="###" onclick="showAtRight(&#39;productList.jsp&#39;)"><i class="fa fa-list-alt"></i> 产品列表</a></li>
            </ul>
            <li><a href="#recordMeun" class="nav-header menu-first collapsed" data-toggle="collapse">
              <i class="fa fa-file-text"></i>  订单管理 <span class="sr-only">(current)</span></a>
            </li> 
            <ul id="recordMeun" class="nav nav-list collapse menu-second">
              <li><a href="###" onclick="showAtRight(&#39;recordList.jsp&#39;)" ><i class="fa fa-list"></i> 订单列表</a></li>
            </ul>
          </ul>
        </p>
      </p>
    </p>
<!-- 右侧内容展示==================================================  -->      
         <p class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header"><i class="fa fa-cog fa-spin"></i> 控制台<small>   欢迎使用XXX后台管理系统</small></h1>
            <!-- 载入左侧菜单指向的jsp(或html等)页面内容 -->
             <p id="content">
                <h4>          
                  <strong>使用指南:</strong><br>
                  <br><br>默认页面内容……
                </h4>                 
             </p> 
        </p> 
    <script type="text/javascript">
    /*
     * 对选中的标签激活active状态,对先前处于active状态但之后未被选中的标签取消active
     * (实现左侧菜单中的标签点击后变色的效果)
     */
    $(document).ready(function () {
      $('ul.nav > li').click(function (e) {
        //e.preventDefault();  加上这句则导航的<a>标签会失效
        $('ul.nav > li').removeClass('active');
        $(this).addClass('active');
      });
    });
    /*
     * 解决ajax返回的页面中含有javascript的办法:
     * 把xmlHttp.responseText中的脚本都抽取出来,不管AJAX加载的HTML包含多少个脚本块,我们对找出来的脚本块都调用eval方法执行它即可
     */
    function executeScript(html)
    {
      var reg = /<script[^>]*>([^\x00]+)$/i;
      //对整段HTML片段按<\/script>拆分
      var htmlBlock = html.split("<\/script>");
      for (var i in htmlBlock) 
      {
        var blocks;//匹配正则表达式的内容数组,blocks[1]就是真正的一段脚本内容,因为前面reg定义我们用了括号进行了捕获分组
        if (blocks = htmlBlock[i].match(reg)) 
        {
          //清除可能存在的注释标记,对于注释结尾-->可以忽略处理,eval一样能正常工作
          var code = blocks[1].replace(/<!--/, '');
          try 
          {
            eval(code) //执行脚本
          } 
          catch (e) 
          {
          }
        }
      }
    }
    /*
     * 利用p实现左边点击右边显示的效果(以id="content"的p进行内容展示)
     * 注意:
     *  ①:js获取网页的地址,是根据当前网页来相对获取的,不会识别根目录;
     *  ②:如果右边加载的内容显示页里面有css,必须放在主页(即例中的index.jsp)才起作用
     *  (如果单纯的两个页面之间include,子页面的css和js在子页面是可以执行的。 主页面也可以调用子页面的js。但这时要考虑页面中js和渲染的先后顺序 )
    */
    function showAtRight(url) {
      var xmlHttp;
      if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlHttp=new XMLHttpRequest();  //创建 XMLHttpRequest对象
      }
      else {
        // code for IE6, IE5
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlHttp.onreadystatechange=function() {    
        //onreadystatechange — 当readystate变化时调用后面的方法
        if (xmlHttp.readyState == 4) {
          //xmlHttp.readyState == 4  ——  finished downloading response
          if (xmlHttp.status == 200) {
            //xmlHttp.status == 200    ——  服务器反馈正常      
            document.getElementById("content").innerHTML=xmlHttp.responseText;  //重设页面中id="content"的p里的内容
            executeScript(xmlHttp.responseText);  //执行从服务器返回的页面内容里包含的JavaScript函数
          }
          //错误状态处理
          else if (xmlHttp.status == 404){
            alert("出错了☹  (错误代码:404 Not Found),……!"); 
            /* 对404的处理 */
            return;
          }
          else if (xmlHttp.status == 403) { 
            alert("出错了☹  (错误代码:403 Forbidden),……"); 
            /* 对403的处理 */ 
            return;
          }
          else {
            alert("出错了☹  (错误代码:" + request.status + "),……"); 
            /* 对出现了其他错误代码所示错误的处理  */
            return;
          }  
        } 
       }
      //把请求发送到服务器上的指定文件(url指向的文件)进行处理
      xmlHttp.open("GET", url, true);    //true表示异步处理
      xmlHttp.send();
    }    
    </script>
  </body>
</html>
Copy after login

Note:

If the icon of FontAwesomt is not displayed correctly, it is probably font-awesome The font file address in .css is wrong

Please check whether the URL in the red box on the right corresponds to the path of the fonts-awesome file in the red box on the left

Two:

userList.jsp page code:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <p>
    我是用户列表 (っ´Ι`)っ
  </p>
</body>
</html>
Copy after login
productList.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <p>
    我是产品列表 ╰( ̄▽ ̄)╭
  </p>
</body>
</html>
Copy after login
recordList.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <p>
    我是订单列表 <( ̄ˇ ̄)/
  </p>
</body>
</html>
Copy after login
These three This is just a simple demonstration page, so I won’t waste any more words

I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How ajax implements remote communication function

##jQuery.ajaxWebService requests WebMethod to handle Ajax

The above is the detailed content of Ajax+div page layout effect. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Users encounter rare glitches: Samsung Watch smartwatches suddenly experience white screen issues Users encounter rare glitches: Samsung Watch smartwatches suddenly experience white screen issues Apr 03, 2024 am 08:13 AM

You may have encountered the problem of green lines appearing on the screen of your smartphone. Even if you have never seen it, you must have seen related pictures on the Internet. So, have you ever encountered a situation where the smart watch screen turns white? On April 2, CNMO learned from foreign media that a Reddit user shared a picture on the social platform, showing the screen of the Samsung Watch series smart watches turning white. The user wrote: "I was charging when I left, and when I came back, it was like this. I tried to restart, but the screen was still like this during the restart process." Samsung Watch smart watch screen turned white. The Reddit user did not specify the smart watch. Specific model. However, judging from the picture, it should be Samsung Watch5. Previously, another Reddit user also reported

How to copy a page in Word How to copy a page in Word Feb 20, 2024 am 10:09 AM

Want to copy a page in Microsoft Word and keep the formatting intact? This is a smart idea because duplicating pages in Word can be a useful time-saving technique when you want to create multiple copies of a specific document layout or format. This guide will walk you through the step-by-step process of copying pages in Word, whether you are creating a template or copying a specific page in a document. These simple instructions are designed to help you easily recreate your page without having to start from scratch. Why copy pages in Microsoft Word? There are several reasons why copying pages in Word is very beneficial: When you have a document with a specific layout or format that you want to copy. Unlike recreating the entire page from scratch

Kyushu Fengshen Assassin 4S Radiator Review Air-cooled 'Assassin Master' Style Kyushu Fengshen Assassin 4S Radiator Review Air-cooled 'Assassin Master' Style Mar 28, 2024 am 11:11 AM

Speaking of ASSASSIN, I believe players will definitely think of the master assassins in "Assassin's Creed". They are not only skilled, but also have the creed of "devoting themselves to the darkness and serving the light". The ASSASSIN series of flagship air-cooled radiators from the appliance brand DeepCool coincide with each other. Recently, the latest product of this series, ASSASSIN4S, has been launched. "Assassin in Suit, Advanced" brings a new air-cooling experience to advanced players. The appearance is full of details. The Assassin 4S radiator adopts a double tower structure + a single fan built-in design. The outside is covered with a cube-shaped fairing, which has a strong overall sense. It is available in white and black colors to meet different colors. Tie

Exquisite light and shadow art in spring, Haqu H2 is the cost-effective choice Exquisite light and shadow art in spring, Haqu H2 is the cost-effective choice Apr 17, 2024 pm 05:07 PM

With the arrival of spring, everything revives and everything is full of vitality and vitality. In this beautiful season, how to add a touch of color to your home life? Haqu H2 projector, with its exquisite design and super cost-effectiveness, has become an indispensable beauty in this spring. This H2 projector is compact yet stylish. Whether placed on the TV cabinet in the living room or next to the bedside table in the bedroom, it can become a beautiful landscape. Its body is made of milky white matte texture. This design not only makes the projector look more advanced, but also increases the comfort of the touch. The beige leather-like material adds a touch of warmth and elegance to the overall appearance. This combination of colors and materials not only conforms to the aesthetic trend of modern homes, but also can be integrated into

Huntkey MX750P full module power supply review: 750W of concentrated platinum strength Huntkey MX750P full module power supply review: 750W of concentrated platinum strength Mar 28, 2024 pm 03:20 PM

With its compact size, the ITX platform has attracted many players who pursue the ultimate and unique beauty. With the improvement of manufacturing processes and technological advancements, both Intel's 14th generation Core and RTX40 series graphics cards can exert their strength on the ITX platform, and gamers also There are higher requirements for SFX power supply. Game enthusiast Huntkey has launched a new MX series power supply. In the ITX platform that meets high-performance requirements, the MX750P full-module power supply has a rated power of up to 750W and has passed 80PLUS platinum level certification. Below we bring the evaluation of this power supply. Huntkey MX750P full-module power supply adopts a simple and fashionable design concept. There are two black and white models for players to choose from. Both use matte surface treatment and have a good texture with silver gray and red fonts.

How to deal with the problem that Laravel page cannot display CSS correctly How to deal with the problem that Laravel page cannot display CSS correctly Mar 10, 2024 am 11:33 AM

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

How to implement page jump in 3 seconds: PHP Programming Guide How to implement page jump in 3 seconds: PHP Programming Guide Mar 25, 2024 am 10:42 AM

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

Colorful Hidden Star P15 24 Review: A hard-core all-round gaming laptop with both good looks and performance Colorful Hidden Star P15 24 Review: A hard-core all-round gaming laptop with both good looks and performance Mar 06, 2024 pm 04:40 PM

In the current era of rapid technological development, laptops have become an indispensable and important tool in people's daily life and work. For those players who have high performance requirements, a laptop with powerful configuration and excellent performance can meet their hard-core needs. With its excellent performance and stunning design, the Colorful Hidden Star P15 notebook computer has become the leader of the future and can be called a model of hard-core notebooks. Colorful Hidden Star P1524 is equipped with a 13th generation Intel Core i7 processor and RTX4060Laptop GPU. It adopts a more fashionable spaceship design style and has excellent performance in details. Let us first take a look at the features of this notebook. Supreme equipped with Intel Core i7-13620H processing

See all articles