html 模板标签中的 id 属性

id attribute in html template tag

因此,我正在深入研究 HTML5 模板标记,作为将来可能解决我的 Web 应用程序问题的方法。我似乎没有找到的一件事是:你能在模板标签中使用 id 属性吗?

模板可以重复使用,但id不能。所以多次使用同一个模板(这就是为什么他们首先被发明的原因)理论上会导致无效 HTML。有官方解答吗?

Can you use the id attribute inside the template tag.

是的,但是:

The template can be reused, but id's can't be.

正确。

So using the same template multiple times (which is why they were invented in the first place) will result in invalid HTML, theoretically.

是的。不要那样做。在将它们添加到主 DOM 之前,请确保 ID 是唯一的。在将模板附加到主 DOM.

之前,您可以在模板的 DOM 中编辑它们

如果您将模板标记到主文档中,ID 必须是唯一的,但是,如果您将模板标记到影子根中,那么您可以在影子中重复使用 ID DOM .