简体   繁体   English

单击菜单列表时如何在同一页面的DIV中加载HTML页面?

[英]How to load HTML page in DIV of the same page when clicking the menu list?

I am trying to convert an html5 example I saw from the web but I am having trouble making the href to load on the same page in the container section, instead of loading into a new page. 我正在尝试转换从网上看到的html5示例,但是我无法使href加载到容器部分的同一页面上,而不是加载到新页面中。 I added a jquery script to override the default behaviour of the href and load it to the div but still doesn't seem to work. 我添加了一个jQuery脚本来覆盖href的默认行为并将其加载到div中,但似乎仍然无法正常工作。 Below is my HTML and JS codes. 以下是我的HTML和JS代码。

INDEX.HTML INDEX.HTML

    <!DOCTYPE html>
    <html >
      <head>
        <meta charset="UTF-8">
        <title>MY PROJECT</title>


        <link rel="stylesheet" href="css/normalize.css">

        <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>

            <link rel="stylesheet" href="css/style.css">

      </head>

      <body>

            <div id="wrapper">
            <div class="overlay"></div>

            <!-- Sidebar -->
            <nav class="navbar navbar-inverse navbar-fixed-top" id="sidebar-wrapper" role="navigation">
                <ul class="nav sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="#">
                           MY PROJECT
                        </a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-fw fa-home"></i> Back to Home</a>
                    </li>
                    <li>
                        <a href="1.html"><i class="fa fa-fw fa-cog"></i> HTML 1</a>
                    </li>
                    <li>
                        <a href="2.html"><i class="fa fa-fw fa-cog"></i> HTML 2</a>
                    </li>
                    <li>
                        <a href="3.html"><i class="fa fa-fw fa-cog"></i> HTML 3</a>
                    </li>
                    <li>
                        <a href="4.html"><i class="fa fa-fw fa-cog"></i> HTML 4</a>
                    </li>
                    <li>
                        <a href="5.html"><i class="fa fa-fw fa-cog"></i> HTML 5</a>
                    </li>
                    <li>
                        <a href="6.html"><i class="fa fa-fw fa-cog"></i> HTML 6</a>
                    </li>
                    <li class="dropdown">
                      <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-fw fa-plus"></i> ADVANCE <span class="caret"></span></a>
                      <ul class="dropdown-menu" role="menu">
                        <li class="dropdown-header">Advance</li>
                        <li><a href="advance.html">chart mod a</a></li>

                      </ul>
                    </li>

                    <li>
                        <a href="#"><i class="fa fa-fw fa-dropbox"></i> SLIDE SHOW MODE</a>
                    </li>

                </ul>
            </nav>
            <!-- /#sidebar-wrapper -->

            <!-- Page Content -->
            <div id="page-content-wrapper">
              <button type="button" class="hamburger is-closed animated fadeInLeft" data-toggle="offcanvas">
                <span class="hamb-top"></span>
                <span class="hamb-middle"></span>
                <span class="hamb-bottom"></span>
              </button>
                <div class="container">
                    <div class="row">
                        <div class="col-lg-8 col-lg-offset-2">
                            <h1 class="page-header">Reports Control</h1>  
                            <p class="lead">(BETA) </p>
                            <p>Use nav side bar to select report ...</p>

                        </div>
                    </div>
                </div>
            </div>
            <!-- /#page-content-wrapper -->

        </div>
        <!-- /#wrapper -->
        <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script>

<script src="js/index.js"></script>

</body>
</html>

INDEX.JS INDEX.JS

 $(document).ready(function () {
  var trigger = $('.hamburger'),
      overlay = $('.overlay'),
     isClosed = false;

    trigger.click(function () {
      hamburger_cross();      
    });

    function hamburger_cross() {

      if (isClosed == true) {          
        overlay.hide();
        trigger.removeClass('is-open');
        trigger.addClass('is-closed');
        isClosed = false;
      } else {   
        overlay.show();
        trigger.removeClass('is-closed');
        trigger.addClass('is-open');
        isClosed = true;
      }
  }

  $('[data-toggle="offcanvas"]').click(function () {
        $('#wrapper').toggleClass('toggled');
  });

  $('#wrapper').on('click', function (e) {
            e.preventDefault();
            var page = $(this).attr('href');
            $('#page-content-wrapper').load(page);
  });

});

$('#wrapper') is a DIV not the anchor, means that there is no href attributes exist. $('#wrapper')是DIV而不是锚,这意味着不存在href属性。 Instead try used : 而是尝试使用:

// $('#wrapper .nav a') - will look for <a> tag inside of wrapper div at the .nav element
$('#wrapper .nav a').on('click', function (e) {
    e.preventDefault();
    var page = $(this).attr('href');
    $('#page-content-wrapper').load(page);
});

The problem is that your event listener is bound to the #wrapper element which has no href attribute. 问题是您的事件侦听器绑定到没有href属性的#wrapper元素。 Inside the event handler function, $(this) refers to the element the handler was bound to, so you need to listen to clicks on the individual <a> elements. 在事件处理函数中, $(this)指向处理程序绑定到的元素,因此您需要侦听单个<a>元素的单击。

$('.nav a').on('click', function (e) {
            e.preventDefault();
            var page = $(this).attr('href');
            $('#page-content-wrapper').load(page);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击菜单中的Li将HTML页面加载到DIV中? - How to load a HTML-page into a DIV by clicking a Li inside a menu? 如何通过单击“ include(&#39;menu.html&#39;)”菜单内的Li来将HTML页面加载到DIV中 - How to load a HTML-page into a DIV by clicking a Li from inside a menu on an 'include('menu.html')' 如何通过单击菜单中的Li将HTML页面从href加载到div中? - How to load a html page from href into a div by clicking a Li inside a menu? 单击链接时如何将整个页面加载到div中? - How to load whole page into a div when clicking a link? 如何在同一页面中加载远程HTML页面链接 <div> 容器? - How to load remote html page links in the same <div> container? 单击另一个页面上的按钮时如何在页面上创建一个div - How to create a div on a page when clicking a button on another page 单击菜单列表中的任何选项时,执行 url 的目标位于同一 html 文件中的另一个 div 标签内 - when clicking any option from menu list, the target of executing the url is inside another div tag in the same html file 如何在点击时将单独的html页面加载到div中? - How to load separate html page into a div on click? 如何在页面加载时将HTML插入div? - How to insert HTML into div on page load? 如何通过单击html页面中的菜单从jsp调用Java方法? - How to call a java method from jsp by clicking a menu in html page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM