layui點擊左側導航欄,實現不刷新整個頁面,只刷新局部的方法


點擊左側菜單欄只刷新局部,局部就用iframe。

首先先建layout頁,建左側菜單欄,然后下面的@RenderBody()

 
 1 <div class="layui-side layui-bg-black">
 2    <div class="layui-side-scroll">
 3      <!-- 左側導航區域(可配合layui已有的垂直導航) -->
 4      <ul class="layui-nav layui-nav-tree" lay-filter="test">
 5  
 6        <li class="layui-nav-item layui-nav-itemed Card">
 7          <a class="" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >名片審核</a>
 8        </li>
 9  
10        <li class="layui-nav-item Project">
11          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >項目審核</a>
12        </li>
13  
14        <li class="layui-nav-item Funds"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >需求審核</a></li>
15      </ul>
16    </div>
17 </div>
18 <div class="layui-body">
19   @RenderBody()
20   @RenderSection("scripts", required: false)
21 </div>

然后在最后寫上js用於點擊跳轉,上面的@RenderSection("scripts", required: false)和下面的#demoAdmin都在另一個頁面中,也就是放iframe的頁面

 1  <script>
 2     //JavaScript代碼區域
 3     layui.use('element', function () {
 4       var $ = layui.jquery
 5         , element = layui.element;     
 6     });
 7     $(".Card").click(function () {
 8       $("#demoAdmin").attr("src", "/Admin/CardManagement")
 9     });
10     $(".Project").click(function () {
11       $("#demoAdmin").attr("src", "/Admin/ProjectManagement")
12     });
13     $(".Funds").click(function () {
14       $("#demoAdmin").attr("src", "/Admin/FundsManagement")
15     });
16   </script>

新建HomeController,新建Index頁面

 1 @{
 2   Layout = null;
 3   Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
 4 }
 5  
 6 <iframe src="/Admin/CardManagement" frameborder="0" id="demoAdmin" style="width: 100%; height: 100%;"></iframe>
 7  
 8  
 9 @section scripts{
10   <script>
11   reHeight();
12   $(window).resize(function () {
13     reHeight();
14   });
15   //設置iframe高度
16   function reHeight() {
17   var bodyHeight = $(window).height();
18   bodyHeight = bodyHeight - 107;
19   if (bodyHeight<400) {
20     bodyHeight = 400;
21   }
22   $("#demoAdmin").height(bodyHeight);
23   }
24   </script>

引用一下上面的layout。

其他頁面引用Layout = "~/Areas/Admin/Views/Shared/_LayoutPage.cshtml";

我把css和js引用都放在了另一個layout里,所以我需要引用一下,否則頁面會亂。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM