How to store nav bar in one file?

前端 未结 5 1572
囚心锁ツ
囚心锁ツ 2021-01-30 11:39

I have a navigation bar for my website:

    
  • Home
5条回答
  •  广开言路
    2021-01-30 11:54

    You could use AJAX and then make a separate page with just the menu code on there.

    Actually you could simply use jQuery's .load() which is very easy to use.

    Here's an example loading nav.html into a div with #nav-container for the ID:

    
    

    You would just have to add this code to each page and have a div on there but once you do this, you'll only have to update nav.html

    Note: This is the best way to do it if you can't use PHP, as iframes are just not that good anymore. This will actually load the code from nav.html into the page right as the page is loading. (accept the , , and other page tags. It'll only the code within on the nav.html page.

    For your users without JavaScript, just put an iframe in tags if you really want to. If you users have to have JavaScript enabled regardless to use your site then you don't have to bother with that. (remember with iframes you have to set targets and stuff on all of the links in the navigation.)

    Remember to include the jQuery file link in your head tags on all of your pages that you're loading nav.html into if you haven't already.

    Hope this is helpful.

提交回复
热议问题