简体   繁体   English

如何通过单击菜单中的Li将HTML页面加载到DIV中?

[英]How to load a HTML-page into a DIV by clicking a Li inside a menu?

i've came across a really annoying issue. 我遇到了一个非常烦人的问题。

So, my plan is to have a UL-menu containing different ammounts of Li inside of it. 因此,我的计划是在其中包含一个包含不同李的李的UL菜单。 And when i click each of them i want to load a new HTML-page into my "Content-DIV". 当我点击它们中的每一个时,我想在我的“Content-DIV”中加载一个新的HTML页面。

I've done alot of research and i found out about Ajax and Jquery. 我做了很多研究,我发现了Ajax和Jquery。 I've tried so many different codes to make it work, and i've managed to link my .js file to my index file sucessfully. 我已经尝试了很多不同的代码来使它工作,我已经成功地将我的.js文件链接到我的索引文件。 I tried it out with a simpel Alert command. 我用simpel Alert命令试了一下。 But then when i try to load my page into my DIV, it doesnt work at all, and i have no idea whats wrong! 但是当我尝试将我的页面加载到我的DIV中时,它根本不起作用,我不知道什么是错的! I found a link here on stackoverflow which were almost the same, tho they didnt use a menu to open up the pages. 我在stackoverflow上找到了一个几乎相同的链接,因为他们没有使用菜单来打开页面。

So here is my index: 所以这是我的索引:

<div id="Left">
    <ul id="nav" >
        <li><a href="Home.html id="load_home">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a>
    </ul>
</div>

<div id="content">


</div>

<script>$(document).ready( function() {
    $("#load_home").on("click", function() {
        $("#content").load("Home.html");
    });
 });
</script>

Any ideas? 有任何想法吗? Any answers would be much appreciated. 任何答案将不胜感激。 Thank you. 谢谢。

/Martin /马丁

You wrote the jQuery code in wrong manners Also please make sure you've are calling jQuery library file. 你用错误的方式编写了jQuery代码另外请确保你正在调用jQuery库文件。

JavaScript code JavaScript代码

$(document).ready(function() {
  $('a').click(function(e) {
    e.preventDefault();
    $("#content").load($(this).attr('href'));
  });
});

HTML Code HTML代码

<div id="Left">
  <ul id="nav">
    <li><a href="page1.html">Home</a></li>
    <li><a href="page2.html">Page 1</a></li>
    <li><a href="page3.html">Page 2</a></li>
  </ul>
</div>
<div id="content">The page will display here</div>

you will try this code instead 你会试试这个代码

$(document).ready( function() {
    $.get('Home.html', function(data) {
        $("#content").html(data);
    });
});

well from your above code i couldn't see you have created any DIV with the ID : - content So jquery is ok but from the above code i can say....jquery is not able to find the #content div..... 从上面的代码我不能看到你已经创建任何ID与ID: - 内容所以jquery是好的,但从上面的代码我可以说.... jquery无法找到#content div ... ..

Please see below example which might help in your case : - 请参阅以下示例,这可能对您的案例有所帮助: -

<ul>
    <li><a id="page1" href="#">About</a></li>
    <li><a id="page2" href="#">Community</a></li>
    <li><a id="page3" href="#">Sponsor</a></li>
</ul>
<div id="result" style="clear:both;">
</div>

At the head part, we need to include JQuery library. 在头部,我们需要包含JQuery库。

<script src="http://code.jquery.com/jquery-1.4.min.js" type="text/javascript"></script>

Add the following code to head part. 将以下代码添加到head部分。

<script type="text/javascript">
    $(document).ready(function(){
       $("#page1").click(function(){
        $('#result').load('pages/page1.html');
         //alert("Thanks for visiting!");
       }); 

       $("#page2").click(function(){
        $('#result').load('pages/page2.html');
         //alert("Thanks for visiting!");
       });
     });
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM