通过渐进增强保持 DRY
我正在使用非常少量的 Javascript 构建一个网站,只是为了向页面添加内容(例如联系表单),而无需转到新页面。
我知道无论如何我都应该构建联系页面(以防万一用户没有打开 javascript),然后使用 javascript(如果他们已经打开)。
那么,如果我不想将表单的 HTML 存储在两个地方,我应该将其存储在哪里呢?
(通常我不会那么挑剔,但我对这个很好奇。)
I'm building a website with very small amounts of Javascript, just to add things to the page (like a contact form) without having to go to a new page.
I understand that I should build the contact page anyways (just in case the user doesn't have javascript turned on) and then use javascript if they've got it.
So where do I store the HTML for the form if I don't want to have it in two places?
(Normally I'm not so picky, but I'm curious on this one.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有权访问服务器端语言,则可以在外部页面中保留表单的单独片段。然后,您可以通过适当的
include
调用将该代码段包含到 HTML 内容页面中。这还有一个额外的好处,即对于 JavaScript,您可以使用 AJAX 从此代码段文件中提取联系表单。事实上,许多插件允许您显示带有 HTML 内容的 DHTML 窗口。例如,查看 ThickBox。如果没有服务器端语言,您可以对框架执行类似的操作。当您需要引用表单片段时,只需在框架中显示它即可。就我个人而言,我不太喜欢框架,因此这对我来说不是一个非常有吸引力的解决方案,但如果您选择(并适当设置框架的样式),您可以使用它。
If you have access to a server-side language, you can keep a separate snippet of the form in an external page. Then, you can include the snippet into the HTML content page with an appropriate
include
call. This has the added benefit that, for your JavaScript, you can pull the contact form from this snippet file using AJAX. In fact, many plugins allow you to display DHTML windows with HTML content. For example, check out ThickBox.Without a server-side language, you can do something similar with frames. Just display the form snippet in a frame when you need to reference it. Personally, I don't like frames very much, so this isn't a very attractive solution for me, but you can use it if you choose (and style the frames appropriately).
只需将联系表单的 HTML 放入 .html 文件中即可。假设您使用 PHP 或其他东西,只需将该文件包含在您的联系页面中,并将其包含在动态联系表单的部分中。该表单仍应提交到同一服务器端页面,并具有相同的外观和感觉。
例如 contactForm.html
Just put your HTML for the contact form in a .html file. Assuming you're using PHP or something, just include the file in your contact page and include it in the section for your dynamic contact form. The form should still submit to the same server-side page and have the same look and feel..
e.g. contactForm.html