【问题标题】:Does it make sense to declare a doctype for an HTML fragment?为 HTML 片段声明 doctype 是否有意义?
【发布时间】:2018-05-13 15:49:59
【问题描述】:

如果我有一个 HTML5 组件/WebComponent,它有一个定义 <template>component.html 文件,那么在 component.html 中声明 <!DOCTYPE html> 是否有意义?也就是说,以下哪一项是最佳做法?

<template>
    <!-- some html content -->
</template>

或者

<!DOCTYPE html>
<template>
    <!-- some html content -->
</template>

【问题讨论】:

  • 模板不是 HTML 页面,而只是 HTML 的一小部分。所以不需要 Doctype。您的代码将读取模板并将其插入到由 HTML 页面提供的现有 DOM 中。我假设您已经为该页面提供了 Doctype。

标签: html html-parsing web-component html5-template html-imports


【解决方案1】:

在所有支持 template 元素的现代浏览器中,两者都将以相同的方式进行解析和处理。

一个是几个八位字节!

所以这取决于您是否更喜欢速度而不是严格的标准合规性

遵循 KISS 原则:我编写(和阅读)的代码越少,我的感觉就越好;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-26
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    相关资源
    最近更新 更多