简体   繁体   English

HTML模板(自定义)标签

[英]HTML Template (Custom) Tag

I understand that using custom html tags is improper for a variety of reasons, but I wanted to run a specific situation by you that might warrant a custom html tag and hopefully get told otherwise or possibly a better way of achieving my goal. 我了解使用自定义html标签的原因多种多样,但我想根据您的情况运行特定情况,可能需要使用自定义html标签,并希望得到其他告知,或者可能是达到目标的更好方法。

Throughout my code I have what I term as templates that are made up of a div tag with a template and a hidden class attached to it. 在我的整个代码中,我称之为模板,它们是由div标签和附加的模板以及一个隐藏的类组成的。 This is not visible on the screen, but basically these "template" tags contains html that I use in Javascript to create a variety of different items. 这在屏幕上不可见,但是基本上这些“模板”标签包含我在Javascript中用于创建各种不同项目的html。 I do this so that I can style my templates in html rather than have to worry about mixing CSS in with my Javascript. 我这样做是为了可以在html中设置模板样式,而不必担心将CSS与Javascript混合在一起。

<!-- TEMPLATE -->
<div class="template hidden">
    <span>Random Container</span>
    <a href="#">Random Button</a>
</div>

In javascript I would do something like 在JavaScript中,我会做类似的事情

var template = document.getElementById("template");
var clone = template.cloneNode(true);
clone.removeClass("template hidden");

I would rather be able to do something like this 我宁愿能够做这样的事情

<template class="hidden">
   <span>Random Container</span>
   <a href="#">Random Button</a> 
</template>

So that if I have multiple templates in a single div I can grab them all rather than having to give them unique class names. 这样,如果我在一个div中有多个模板,则可以全部使用它们,而不必给它们提供唯一的类名。 Of course my reasoning for needing an implementation goes a lot deeper than this, but its not necessary to waste your time with the details. 当然,我需要实现的理由比这要深得多,但不必浪费您的时间来了解细节。 Let's just say that it will help clean up my Javascript ALOT. 只是说这将有助于清理我的Javascript。

Because the custom template tag is hidden and really is nothing more than a container that is convenient to call within javascript with document.getElementsByTagName("template"); 因为自定义模板标签是隐藏的,实际上仅是一个容器,可以方便地在JavaScript中使用document.getElementsByTagName(“ template”);进行调用。 Is this ok to do? 这样可以吗? I would probably prefix the tag with a custom name in case template ever gets implemented into html. 我可能会以自定义名称为标记添加前缀,以防模板被实现为html。

Modern browsers generally “support” custom tags in the sense of parsing them and constructing DOM nodes, so that the elements can be styled and processed in scripting. 现代浏览器通常在解析自定义标签和构造DOM节点的意义上“支持”自定义标签,以便可以在脚本中对元素进行样式设置和处理。

The main problem is IE prior to IE 9, but it can be handled using document.createElement('...') once for each custom tag name. 主要问题是IE 9之前的IE,但对于每个自定义标签名称,都可以使用document.createElement('...')一次。

Another problem is that validators will report the tags as errors, and if there are loads of such errors, you might not notice some real errors in markup. 另一个问题是,验证器会将标签报告为错误,如果有大量此类错误,您可能不会注意到标记中的某些实际错误。 In principle you can create your own DTD to deal with this (I have an HTML DTD generator under construction, but it is trickier than I expected...). 原则上,您可以创建自己的DTD来处理此问题(我正在构建HTML DTD生成器,但是它比我预期的要复杂...)。

With these reservations, use custom tags if they essentially simplify your job as compared with using classes. 有了这些保留,如果与使用类相比,它们实质上简化了您的工作,请使用自定义标签。

Why not use one of HTML5's data attributes ? 为什么不使用HTML5的数据属性之一 They are for storing private data or custom info. 它们用于存储私人数据或自定义信息。

For your case, you could add data-type="template" or data-name="template" and then search and remove based on that. 对于您的情况,您可以添加data-type="template"data-name="template" ,然后基于此搜索并删除。 One simple function just like you would write to remove your <template> tag, but without breaking rules. 一个简单的函数就像您编写删除<template>标记一样,但是没有违反规则。

So, using your example, <div data-type="template" class="hidden"></div> 因此,使用您的示例<div data-type="template" class="hidden"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM