我们怎样才能避免页眉和页脚代码在每个 html 页面中重复
我有大约十个 html 页面,每个页面也有相同的页眉和页脚标记,我可以有一个包含完整页眉和页脚的页面吗?我将从其他 html 页面引用该特定页面。
I have some ten html pages each page also has same header and footer tag can i have one page with complete header and footer ,and i will refer to that particular page from other html pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您不关心禁用了 JavaScript 的用户,或者正在使用某些移动平台,则可以使用 JavaScript 来完成此操作。
headerfooter.js
page.html
但不要这样做,它对用户不友好,而且不专业。只需坚持使用 php,或构建一个稍后不需要编辑的可靠模板即可。
If you don't care about users who have JavaScript disabled, or are using some mobile platforms, you can use JavaScript to do it.
headerfooter.js
page.html
But don't do this, it's user unfriendly, and unprofessional. Just stick with either using php, or building a solid template which doesn't need to be edited much later.
您可以通过使用服务器端语言(例如 PHP 或其他无数不同语言中的一种)来完成此操作,这些语言会进行预处理页面。沿着这些思路:
You do this by using a server side language like PHP or another one of the myriad different languages out there, which pre-processes the page. Something along these lines:
您的服务器端脚本语言是什么?您可以执行所谓的“包含”。
确切的语法取决于您的 Web 服务器支持的语言。
What's your server side scripting language? You can do what is called an "include."
The exact syntax depends on the language(s) your web server supports.
假设问题上的“master-pages”标签指的是 ASP.NET,这里有一个 超级链接。
诗。您也应该尝试 Ruby on Rails :)
Presuming the "master-pages" tag on the question refers to ASP.NET, here's a Super Link.
Ps. You should give Ruby on Rails a try as well :)
使用 Django。当你掌握窍门时,这是最好的事情:)
Use Django. The best thing out there when you get a hang of it :)
绝对我们可以通过只定义一个页眉和页脚内容来避免在多个页面上编写相同的代码,例如页眉和页脚,并在所有页面上使用它页。
假设您的index.html 中有页眉和页脚元素,如下所示:
使用JavaScript 将页眉和页脚内容存储在变量中
在您想要添加页眉和页脚的页面上使用此脚本并享受JavaScript 的强大功能。
我们可以更深入地讨论代码重复这个主题,其中有很多缺点(缺点)和较少的优点(优点)。
JavaScript 是最好的解决方案,因为不需要服务器端脚本,只需要客户端脚本,是的,要实现此功能,您的客户端浏览器必须启用 JavaScript< /strong> 这是当今主流浏览器的默认设置。
Absolutely we can Avoid Writing same code like header and footer on multiple pages by having just a single header and footer content defined and use it on all pages.
Lets assume you have header and footer elements in your index.html for example as shown below:
Use JavaScript to store header and footer contents in a variable
Use this script on pages you would like to add header and footer and enjoy the power of JavaScript.
We can discuss this topic of Code Duplication in much greater depth with lots of cons(Disadvantages) and Less pros(Advantages).
JavaScript is the best solution because no server-side scripting needed, only Client side scripting and yeah for this to work, your client side browsers must have JavaScript Enabled that's default for major browsers nowadays.