简体   繁体   English

如何从菜单中预加载html链接并使用javascript隐藏/显示内容?

[英]How to preload html links from a menu and hide / show the content with javascript?

I have an html menu with links to other pages which have this menu included too. 我有一个HTML菜单,该菜单具有指向其他包含此菜单的页面的链接。 Very basic: 很基本:

<div id="sidebar-nav">
    <ul id="dashboard-menu">
        <li class="active">
            <div class="pointer">
                <div class="arrow"></div>
                <div class="arrow_border"></div>
            </div>
            <a href="index.html">
                <i class="icon-dashboard"></i>
                <span>Home</span>
            </a>
        </li>
        <li>
            <a href="1.html">
                <i class="icon-signal"></i>
                <span>1</span>
            </a>
        </li>
        <li>
            <a href="2.html">
                <i class="icon-picture"></i>
                <span>2</span>
            </a>
        </li>
        <li>
            <a href="3.html">
                <i class="icon-calendar-empty"></i>
                <span>3</span>
            </a>
        </li>
    </ul>
</div>

So basically this menu helps navigate through content. 因此,基本上,此菜单可帮助您浏览内容。

When I load the home of the menu I want a function to automatically load the other links too so it speeds up the navigation and of course I want the content of those links to show up only when the corresponding link in the menu is clicked. 当我加载菜单的主页时,我也希望函数也自动加载其他链接,以便加快导航速度,当然,我希望这些链接的内容仅在单击菜单中的相应链接时才会显示。

What library / function should I look into to achieve that? 我应该考虑使用什么库/函数来实现?

Which Library or Function to use 使用哪个库或函数

You can see, in my code everything is jQuery. 您可以看到,在我的代码中,所有内容都是jQuery。 So in my advice, jQuery will be the best for you! 因此,根据我的建议,jQuery将是最适合您的! You can have it here: 您可以在这里找到它:

api.jquery.com or even at their site www.jquery.com You can have a quick link at their footer tag. api.jquery.com或什至在其网站www.jquery.com上,您都可以在其页脚标记处获得快速链接。

Pages will be cached 页面将被缓存

Your page and some of its code is saved as a cache in the local storage, you can check them out using F12 . 您的页面及其某些代码被保存为本地存储中的缓存,您可以使用F12检出它们。 Which will open Developer Tools, then in Network workspace, check out which are loaded from Server and which are loaded from local storage. 哪个将打开开发人员工具,然后在“网络”工作区中,检查哪些是从服务器加载的,哪些是从本地存储加载的。 Almost every browser will save some of your code, from .css files or your scripts so that it doesn't have to load it everytime. 几乎每个浏览器都会保存.css文件或脚本中的某些代码,从而不必每次都加载它。 You can see the code 304 (correct me, if mistaken) in browser which would mean that the file was loaded from local machine. 您可以在浏览器中看到代码304(如果出错,请纠正我),这意味着该文件是从本地计算机加载的。 So don't worry about this. 所以不用担心。 You can get other helpfull articles about speeding up and caching the sessions too. 您还可以获得有关加速和缓存会话的其他有用的文章。

Dynamically Creating the menu 动态创建菜单

To dynamically create the link navigation menu you can use: 要动态创建链接导航菜单,可以使用:

Example

$('#dashboard-menu').html();

In the HTML of the code, you can write the list items. 在代码的HTML中,您可以编写列表项。 Which will be set so that you get them. 将进行设置,以便您获得它们。 You can call this function on page start as: 您可以在页面开始时调用此函数,如下所示:

Code Example 代码示例

$(document).ready(function () {
  $('#dashboard-menu').html();
}

Techniques 技术技巧

If you don't know what would be the menu, for example you want to load the data from Database, you can use ajax requests too, this way you will write the result in the dashboard-menu. 如果您不知道菜单是什么,例如想要从数据库中加载数据,则也可以使用ajax请求,这样您就可以将结果写入仪表板菜单。 However, your site won't get fast just by removing the navigation menu and laoding it after the page load. 但是,仅在页面加载后删除导航菜单并对其进行排名,您的网站将无法快速发展。

For example: 例如:

$.ajax({
  url: "page_to_load.html"
  success: function (data) {
    $('#dashboard-menu').html(data);
  }
});

Alternate way of using Ajax 使用Ajax的替代方法

Alternate way for this is load() . 替代方法是load()

http://api.jquery.com/load/ http://api.jquery.com/load/

To show only relative menu 仅显示相对菜单

To show the content you can use show() . 要显示内容,可以使用show() Like this, lets take the example from your code 这样,让我们​​以代码为例

Snippet from your code 您的代码段

<li>
   <a href="1.html">
     <i class="icon-signal"></i>
       <span>1</span>
   </a>
</li>

Showing child 显示孩子

To show the a from this; 为了显示a since a is a child of li , you can use this: 由于ali的孩子,因此可以使用:

$('li').click(function () {
  $(this).find('a').show(); // or even use toggle()
}

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

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