将 html(脚本化)菜单放在单独的文件中
我有一个使用 jquery 创建的菜单,该菜单在某些按钮鼠标悬停时“弹出”。
它有很多选项和子菜单选项,我想将其放入一个单独的文件中,这样只需下载一次,并且不需要每次都重新下载所有 html 标记和文本用户点击我网站上的子页面的时间。
我正在考虑使用 jquery 模板引擎的想法,但无法完全理解实现这一点的最佳方法。这毕竟不是一个模板,而是一个静态菜单。
我很确定我不能将它放入 JS 文件中,因为它本身不是脚本,它是实际的 html 标记(主要是表格)。
有什么想法吗?
I have a menu that I've created using jquery that "pops-up" on certain button mouse-overs.
it has a LOT of options and sub-menu options and I'd like to put it into a separate file so that it only needs to be downloaded once and all of the html markup and text doesn't need to be re-downloaded every time a user hits a sub page on my site.
I was toying with the idea of using the jquery template engine but couldn't quite wrap my head around the best way to work that. This isn't a template after all, but a static menu.
I'm pretty sure I can't put it in a JS file because it's not script per se, it's actual html markup (mostly tables).
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不太确定你的意思,但我倾向于在原始页面标记中包含所有“弹出窗口”的 html,然后简单地将
display
切换为none
或 < code>block 与您想要的任何 JQuery 事件,例如这样您就不必像使用 Ajax 那样每次都下载标记,并且您也可以获得额外标记中的任何内容的 SEO 好处。
如果您确实选择使用 Ajax,您还可以通过在下载结果后缓存结果(基本上保存本地副本以供重复使用)来防止重复下载,但这并不是真正必要的,因为第一种方法更容易且更适合 SEO
not really sure what you mean but what I tend to do is include the html for all "pop-ups" in the original page markup and simply toggle
display
tonone
orblock
with whatever JQuery event you want, such asThis way you dont have to download the markup every time as you would if you were using Ajax and you get the SEO benefits of whatever is in the extra markup too.
If you did choose to use Ajax you could also prevent repeated downloading by caching the results (basically saving a local copy to reuse) after they have been downloaded, but this wouldn't really be necessary because the 1st method is easier and better for SEO
这可能有点迂回,但是将 html 页面设置为 XHTML 文档类型
使用 php 文件扩展名(“index.php”而不是“index.html”),然后您就可以使用
并 长菜单代码。然后,在“scriptedmenufile.php”内放置菜单的 html 代码。
This may be too much of a roundabout, but having your html pages set to an XHTML doctype
and using a php file extension ("index.php" instead of "index.html") instead you would then be able to use
in place of the long menu code. Then, inside "scriptedmenufile.php" you will place the html code for your menu.
我会将其添加到页面底部的隐藏 div 中。然后,当你希望它“弹出”时,我会使用 jquery ui 对话框 http://jqueryui.it/demos/dialog 在该隐藏的 div 上将其弹出到对话框中。
如果您希望将其放在单独的文件中,您可以使用服务器端模板并将其渲染到隐藏的主 html 页面的底部
I would add this into a hidden div on the bottom of your page. Then when you want it to "pop up" I would use jquery ui dialog http://jqueryui.it/demos/dialog on that hidden div to pop it up in a dialog.
If you want it in a separate file you could use a server-side template and render it into the bottom of your main html page hidden
因此,总而言之,这将是单独缓存菜单(或任何单独的 html 元素)的唯一方法,但请记住,这会在用户第一次到达您的网站时生成额外的 http 请求,这可能不值得您花费 50k重新保存与使用服务器端包含或
php include()
So in conclusion this would be the only way to cache the menu individually (or any individual html elements) but bear in mind this would generate an extra http request the first time a user arrives at your site which may not be worth the 50k you're saving versus using serer side includes or
php include()