Home > php教程 > PHP开发 > Implementation of 'left menu, right content' page effect based on Ajax+div

Implementation of 'left menu, right content' page effect based on Ajax+div

高洛峰
Release: 2016-12-24 15:09:42
Original
2282 people have browsed it

Effect demonstration:

①Default page (index.jsp):

Implementation of left menu, right content page effect based on Ajax+div

②: After clicking the user list option under the user management label on the left, the content of the default page on the right is updated to the user list page (userList.jsp) Content:

Implementation of left menu, right content page effect based on Ajax+div

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

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

1:

The sample program used 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)

Using the Bootstrap framework and FontAwesome icon (no way, bootstrap comes with it There are 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 have basically commented it 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>
  <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">
       <div>
        <div>
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" >
            <span>Toggle navigation</span>
            <span></span>
            <span></span>
            <span></span>
          </button>
           <a href="index.jsp">XXXX.com</a>
        </div>
        <div 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>
        </div>
       </div>
    </nav>
  <!-- 左侧菜单选项========================================= -->
    <div>
      <div>
        <div class="col-sm-3 col-md-2 sidebar">    
          <ul class="nav nav-sidebar">
            <!-- 一级菜单 -->
            <li><a href="#userMeun" class="nav-header menu-first collapsed" data-toggle="collapse">
              <i class="fa fa-user"></i>  用户管理 <span>(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>(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>(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>
        </div>
      </div>
    </div>
<!-- 右侧内容展示==================================================  -->      
         <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1><i class="fa fa-cog fa-spin"></i> 控制台<small>   欢迎使用XXX后台管理系统</small></h1>
            <!-- 载入左侧菜单指向的jsp(或html等)页面内容 -->
             <div id="content">
                <h4>          
                  <strong>使用指南:</strong><br>
                  <br><br>默认页面内容……
                </h4>                 
             </div> 
        </div> 
    <script type="text/javascript">
    /*
     * 对选中的标签激活active状态,对先前处于active状态但之后未被选中的标签取消active
     * (实现左侧菜单中的标签点击后变色的效果)
     */
    $(document).ready(function () {
      $(&#39;ul.nav > li&#39;).click(function (e) {
        //e.preventDefault();  加上这句则导航的<a>标签会失效
        $(&#39;ul.nav > li&#39;).removeClass(&#39;active&#39;);
        $(this).addClass(&#39;active&#39;);
      });
    });
    /*
     * 解决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(/<!--/, &#39;&#39;);
          try
          {
            eval(code) //执行脚本
          } 
          catch (e) 
          {
          }
        }
      }
    }
    /*
     * 利用div实现左边点击右边显示的效果(以id="content"的div进行内容展示)
     * 注意:
     *  ①: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"的div里的内容
            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

Attention :

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

Implementation of left menu, right content page effect based on Ajax+div

Please check whether the url in the red box on the right corresponds to the fonts-awesome file in the red box on the left Path

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>
  <div>
    我是用户列表 (っ´Ι`)っ
  </div>
</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>
  <div>
    我是产品列表 ╰( ̄▽ ̄)╭
  </div>
</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>
  <div>
    我是订单列表 <( ̄ˇ ̄)/
  </div>
</body>
</html>
Copy after login

These three are just simple demonstration pages, so I won’t waste more words on the above. The following is the implementation of the "left menu, right content" page effect based on Ajax+div introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!


For more related articles on the implementation of the "left menu, right content" page effect based on Ajax+div, please pay attention to 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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template