在做前端网页的时候,会涉及到很多界面,有的时候,这些界面都会有重复的代码,比如侧边栏菜单的重复代码,头部导航的 重复代码,底部的重复代码,这个时候,为了使每个页面的代码看起来简洁明了,我们需要把这些重复的代码放到公共的页面里面,在具体页面只需引用即可。
主要代码
<div id="head"></div> <div id="side"></div> <script> $("#head").load("com/head.html"); $("#side").load("com/side.html"); </script>
1:在文件里面新建head.html和side.html
2:打开head.html,side.html类似
将头部重复的代码复制在head.html界面里面
head.html
<div class="header-right"> <div class="profile_details_left"><!--notifications of menu start --> <ul class="nofitications-dropdown"> <li class="dropdown head-dpdn"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-bell"></i><span class="badge blue">3</span></a> <ul class="dropdown-menu"> <li><a href="#"> <div class="user_img"><img src="images/p5.png" alt=""></div> <div class="notification_desc"> <p>危险区域报警</p> </div> <div class="clearfix"></div> </a></li> <li class="odd"><a href="#"> <div class="user_img"><img src="images/p6.png" alt=""></div> <div class="notification_desc"> <p>滞留报警</p> </div> <div class="clearfix"></div> </a></li> <li><a href="#"> <div class="user_img"><img src="images/p7.png" alt=""></div> <div class="notification_desc"> <p>sos报警</p> </div> <div class="clearfix"></div> </a></li> <li><a href="#"> <div class="user_img"><img src="images/p8.png" alt=""></div> <div class="notification_desc"> <p>脱岗串岗报警</p> </div> <div class="clearfix"></div> </a></li> <li> </li> </ul> </li> <li class="dropdown head-dpdn"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-tasks"></i><span class="badge blue1">9</span></a> <ul class="dropdown-menu"> <li><a href="#"> <div class="task-info"> <span class="task-desc">南通部分</span> <div class="clearfix"></div> </div> </a></li> <li><a href="#"> <div class="task-info"> <span class="task-desc">无锡部分</span> <div class="clearfix"></div> </div> </a></li> <li><a href="#"> <div class="task-info"> <span class="task-desc">苏州部分</span> <div class="clearfix"></div> </div> </a></li> <li><a href="#"> <div class="task-info"> <span class="task-desc">成都部分</span> <div class="clearfix"></div> </div> </a></li> </ul> </li> </ul> <div class="clearfix"> </div> </div> <!--notification menu end --> <div class="profile_details"> <ul> <li class="dropdown profile_details_drop"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <div class="profile_img"> <span class="prfil-img"><img src="images/p1.png" alt=""> </span> <div class="user-name"> <p>王婷</p> <span>Admin</span> </div> <i class="fa fa-angle-down lnr"></i> <i class="fa fa-angle-up lnr"></i> <div class="clearfix"></div> </div> </a> <ul class="dropdown-menu drp-mnu"> <li> <a href="#"><i class="fa fa-user"></i> 修改密码</a> </li> <li> <a href="#"><i class="fa fa-sign-out"></i>退出登录</a> </li> </ul> </li> </ul> </div> <div class="clearfix"> </div> </div> <div class="clearfix"> </div>
side.html
<div class="menu"> <ul id="menu" > <li id="menu-home" ><a href="index.html"><i class="fa fa-tachometer"></i><span>实时监控</span></a></li> <li><a href="#"><i class="fa fa-cogs"></i><span>数据管理</span><span class="fa fa-angle-right" style="float: right"></span></a> <ul> <li><a href="grids.html">人员管理</a></li> <li><a href="product.html">访客管理</a></li> </ul> </li> <li id="menu-comunicacao" ><a href="#"><i class="fa fa-book nav_icon"></i><span>目标位置</span><span class="fa fa-angle-right" style="float: right"></span></a> <ul id="menu-comunicacao-sub" > <li id="menu-mensagens" style="width: 120px" ><a href="product.html">员工位置</a> </li> <li id="menu-arquivos" ><a href="product.html">访客位置</a></li> <!-- <li id="menu-arquivos" ><a href="icons.html">Icons</a></li>--> </ul> </li> <li><a href="product.html"><i class="fa fa-map-marker"></i><span>轨迹回放</span></a></li> <li id="menu-academico" ><a href="#"><i class="fa fa-file-text"></i><span>电子考勤</span><span class="fa fa-angle-right" style="float: right"></span></a> <ul id="menu-academico-sub" > <li id="menu-academico-boletim" ><a href="login.html">签到设置</a></li> <li id="menu-academico-avaliacoes" ><a href="product.html">签到查询</a></li> </ul> </li> <!-- <li><a href="charts.html"><i class="fa fa-bar-chart"></i><span>11</span></a></li>--> <li><a href="#"><i class="fa fa-envelope"></i><span>告警信息</span><span class="fa fa-angle-right" style="float: right"></span></a> <ul id="menu-academico-sub" > <li id="menu-academico-avaliacoes" ><a href="product.html">员工告警</a></li> <li id="menu-academico-boletim" ><a href="product.html">访客告警</a></li> </ul> </li> <li><a href="#"><i class="fa fa-cog"></i><span>账户管理</span><span class="fa fa-angle-right" style="float: right"></span></a> <ul id="menu-academico-sub" > <li id="menu-academico-avaliacoes" ><a href="product.html">部门管理</a></li> <li id="menu-academico-boletim" ><a href="product.html">用户管理</a></li> <li ><a href="product.html">权限管理</a></li> </ul> </li> <li><a href="#"><i class="fa fa-shopping-cart"></i><span>系统配置</span><span class="fa fa-angle-right" style="float: right"></span></a> <ul id="menu-academico-sub" > <li id="menu-academico-avaliacoes" ><a href="product.html">围栏配置</a></li> <li id="menu-academico-boletim" ><a href="product.html">参数配置</a></li> </ul> </li> </ul> </div>
3:在当前界面(test.html)引入公共代码
test.html
<!DOCTYPE HTML> <html> <head> <title>Home</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all"> <link href="css/style.css" rel="stylesheet" type="text/css" media="all" /> <link href="css/font-awesome.css" rel="stylesheet"> <script src="js/jquery-2.1.1.min.js"></script> <script src="js/bootstrap.js"> </script> </head> <body> <div class="page-container"> <div class="left-content"> <div class="mother-grid-inner"> <!--头部导航导入--> <div class="header-main"> <div id="head"></div> </div> <div class="inner-block"> <div class="climate" style="height: 2123px;"> 内容 </div> </div> </div> </div> <!--侧边栏导入--> <div class="sidebar-menu"> <div id="side"></div> </div> <div class="clearfix"> </div> </div> <script> $("#head").load("com/head.html"); $("#side").load("com/side.html"); </script> </body> </html>
4:这个时候,就化繁为简了,页面代码看上去也就清爽多了。