简体   繁体   English

HTML + jquery-用于更改内容DIV中内容的菜单

[英]HTML + jquery - menu to change contents in the content DIV

I've been digging around on how to do it, but didn't find any solution which would fulfill my needs. 我一直在研究如何做,但是没有找到任何可以满足我需要的解决方案。

Got a basic layout of my page with menu on the left, separate div for header and a div in the middle where all the content should be displayed when one of the menu buttons is selected. 得到了页面的基本布局,左侧菜单,标题的单独div,中间的div,选择了菜单按钮之一后应显示所有内容。

I'd like to change the content only of the "content" div dynamically without reloading the page when one of the menu buttons is pressed. 我想动态更改“内容” div的内容,而无需按下菜单按钮之一时重新加载页面。

I've managed to do it as per below with jquery: 我已经设法做到以下使用jQuery:

        <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){

            $('#nav ul li a').click(function(){
                $('#main').load('contents.html #' + $(this).attr('href'));
                return false;
            });

        });

The menu : 菜单 :

        <nav id="nav">
            <ul class="menu">
                <li class="menu1"><a href="menu1"></a></li>
                <li class="menu2"><a href="menu2"></a></li>
                <li class="menu3"><a href="menu3"></a></li>
                <li class="menu4"><a href="menu4"></a></li>
                <li class="menu5"><a href="menu5"></a></li>
            </ul>
        </div>
    </nav>  

contents.html file sample: contents.html文件样本:

<div id="menu1">
    <h2>Menu1</h2>
    <p>Menu1 page contents</p>
</div>

So when menu1 button is pressed I'm able to change the content of main div to reflect the relevant data - this works. 因此,当按下menu1按钮时,我可以更改main div的内容以反映相关数据-这可行。

My questions are: 我的问题是:

1) is this the correct way on how to do it ? 1)这是正确的方法吗? If not what would you recommend ? 如果没有,您会推荐什么?

2)I want to display more advanced stuff not only text (eg What if I want to have another jquery in the div-id="menu1" ? I've tried it and it didn't work.) 2)我不仅要显示文本,还要显示更高级的内容(例如,如果我想在div-id =“ menu1”中添加另一个jQuery,该怎么办?我已经尝试过了,但是没有用。)

PS - I'm just a beginner so maybe I've missed something basic - but couldn't fix it by myself. PS-我只是一个初学者,所以也许我错过了一些基本知识-但无法自己解决。

Thanks in advance! 提前致谢!

Peter 彼得

I think there are a number of ways to achieve what you want. 我认为有很多方法可以实现您想要的。 It depends on the nature of the content you want to load. 这取决于您要加载的内容的性质。

If there isn't masses of it, you could load it all onto the page on page load within different div's and then just show and hide the relevant div when clicking the menu items. 如果没有大量内容,则可以将其全部加载到不同div上的页面加载中的页面上,然后单击菜单项时仅显示和隐藏相关的div。 (have a look at twitter bootstrap for some implementations, things like tabbed browsing). (有关某些实现,请查看twitter bootstrap ,例如选项卡式浏览)。

If you do just have text, you could use ajax to load in the data from an external file w3schools.com . 如果您只有文字,则可以使用ajax从外部文件w3schools.com加载数据。

Alternatively as mentioned in a comment above, you could create a single page application using something like angular.js , knockout.js etc. 或者,如上面的评论中所述,您可以使用angular.jsknockout.js等创建单个页面应用程序。

Hope that helps. 希望能有所帮助。

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

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