简体   繁体   English

如何使用jquery mobile在多个页面中使用相同的导航栏?

[英]how to use the same navbar in multiple pages with jquery mobile?

Currently i have multiple pages embedded in one html file, and a navbar in the bottom of each page to navigate to another page. 目前,我在一个html文件中嵌入了多个页面,并在每个页面的底部导航到导航到另一个页面。

However, the navbars in different pages are identical codes in different positions. 但是,不同页面中的导航栏是不同位置的相同代码。 Hence, editing a small detail in the navbar mean i have to change (the same) code in all of those positions. 因此,在导航栏中编辑一个小细节意味着我必须在所有这些位置中更改(相同)代码。 Moreover, the highlight of navbar buttons aren't act correctly if I implement it this way. 此外,如果我以这种方式实现导航按钮的突出显示不正确。

Actually, the code for navbar are the same- they are all inside identical div tag. 实际上,导航栏的代码是相同的 - 它们都在相同的div标签内。 Hence, i think there must be some way to reuse one code for all of those navbars? 因此,我认为必须有一些方法可以为所有这些导航栏重用一个代码? So that i don't have to edit the code again and again each time i change some detail in the navbar? 因此,每次我更改导航栏中的某些细节时,我不必一次又一次地编辑代码? (It'd be a disaster if i have 10 pages or more) (如果我有10页或更多,这将是一场灾难)

You can use JavaScript to add a element to each page as it is initialized (make sure this code is included in every HTML document): 您可以使用JavaScript在初始化时为每个页面添加元素(确保此代码包含在每个HTML文档中):

$(document).on('pageinit', '[data-role="page"]', function () {
    $(this).children('[data-role="content"]').append('<div class="my-nav-bar">...</div>');
});

You would then add the HTML for your navbar in the .append() function call. 然后,您将在.append()函数调用中为导航栏添加HTML。 This allows you to keep a single copy of the code and have it be used on every page in the site. 这允许您保留代码的单个副本,并将其用于站点中的每个页面。

This has the added benefit that the code only has to be downloaded once by the user rather than for each page. 这具有额外的好处,即代码只需要由用户而不是每个页面下载一次。

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

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