热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用未知的HTML标签可以吗?-IsitOKtouseunknownHTMLtags?

CorrectmeifImmistaken,butAFAIK,unknownHTMLtagsinmarkup(i.e.tagsnotdefinedintheHTM

Correct me if I'm mistaken, but AFAIK, unknown HTML tags in markup (i.e. tags not defined in the HTML spec, like say, ) will eventually be treated as a regular

in an HTML 5 browser environment.

如果我搞错了,请纠正我,但是在HTML 5浏览器环境中,标记中的未知HTML标记(例如, )最终将被视为常规的

I'm thinking: how supportable is this practice? I mean, if I use unknown HTML tags in my markup, what pitfalls can I expect? Will a velociraptor pounce on me within the next few seconds?

我在想:这种做法有多可行?我的意思是,如果我在标记中使用未知的HTML标记,我能预料到什么缺陷呢?迅猛龙会在几秒钟内扑向我吗?

The reason I ask is that if these tags defer to

, I can potentially use these tags in a more semantic manner than, say, assigning class names that identify modules. Have a look at this article, for example, of a .media class. Now what if instead of writing up that CSS to target .media, I make it target instead? In my opinion, that makes the markup much more readable and maintainable, but I do acknowledge that it's not "correct" HTML.

我这样问的原因是,如果这些标记遵从

,那么我可能会以一种更语义化的方式使用这些标记,而不是指定标识模块的类名。请看这篇文章,例如。media类。现在,如果我不把CSS写在目标。media上,而是把目标设置为 呢?在我看来,这使标记更易于阅读和维护,但我承认它不是“正确的”HTML。

EDIT

Just to be transparent, I did find this SO question from a few years back. It's been closed as off-topic, but I feel that I have a valid point in my own wording. It's a close duplicate, I admit, but it's from a few years back, so there might have been changes in the general environ of opinions across web developers about the topic.

坦率地说,几年前我确实发现了这个问题。这句话以离题的方式结尾,但我觉得我自己的说法是有道理的。我承认,这是一份很接近的副本,但这是几年前的事,所以对于这个话题,web开发人员的普遍看法可能发生了变化。

10 个解决方案

#1


13  

You should always approach HTML as it is defined in its respective specification. "Defining" new tags is a bit of an extreme approach. It might pass a browser check because it implements various failsafes, but there is no guarantee of this. You're entering the land of Undefined Behaviour, at best. Not to mention you will fail validation tests, but you seem to be aware of that.

您应该始终接近HTML,因为它是在其各自的规范中定义的。“定义”新标签有点极端。它可能会通过浏览器检查,因为它实现了各种故障保护,但这并不能保证。你正在进入一个行为不明确的世界。更不用说验证测试会失败,但你似乎已经意识到了这一点。

If you wish to be more semantically expressive in your markup, you can use HTML5 which defines quite a bit of more descriptive tags for describing the structure of your page instead of generic divs which need to be appended ids or classes.

如果您希望在标记中更具语义表达性,您可以使用HTML5,它定义了相当多的描述性标记来描述页面的结构,而不是需要附加id或类的通用div。

In the end, a short answer: No, it's bad practice, you shouldn't do it and there could be unforeseen problems later on in your development.

最后,一个简短的回答:不,这是不好的做法,您不应该这样做,并且在以后的开发中可能会出现不可预见的问题。

#2


49  

user1309389 had a very good answer, and I agree with the appeal to the spec. But I disagree with their conclusion, and I think they're wrong about made-up elements leading to "undefined behaviour". I want to propose an alternative way of thinking about it, rooted in how the spec and browsers actually handle made-up elements.

user1309389有一个很好的答案,我同意对spec的要求,但是我不同意他们的结论,我认为他们对导致“未定义行为”的虚构元素的看法是错误的。我想提出一种考虑它的替代方法,其根源在于规范和浏览器实际如何处理虚构的元素。

It's 2015, we're on the verge of the CustomElement spec being widely adopted, and polyfills are readily available. Now is a great time to be wondering about "making up your own elements". In the near future, you'll be able to create new elements with your own choice of tag and behaviour in a fully standard and supported way that everyone can love. But until this lands in all browsers, and until the majority of people are using supporting browsers, you can take advantage of the hard work of the Polymer or X-Tags projects to check out the future of custom elements in a nearly-standard and mostly-supported way that quite a few people can love. This is probably the "right thing" to do. But it doesn't answer your question, and frankly I find "just use X" or "don't do X" to be less helpful than "here's how the spec covers this, and here's what browsers do". So, here's what I love.

现在是2015年,我们正处在被广泛采用的CustomElement规范的边缘,并且polyfill可以很容易地获得。现在正是思考“构建自己的元素”的好时机。在不久的将来,您将能够用您自己选择的标签和行为创建新的元素,以一种所有人都能喜欢的完全标准和受支持的方式。各浏览器,但直到这土地,直到大多数人使用的是支持的浏览器,您可以利用聚合物的努力或X-Tags项目检查自定义元素的未来nearly-standard mostly-supported方式,相当多的人可以爱。这可能是正确的做法。但它并没有回答你的问题,坦率地说,我发现“使用X”或“不使用X”比“规范如何覆盖这一点,以及浏览器如何做”更没有帮助。这就是我喜欢的。

Against the heartfelt recommendation (and sometimes screaming) of much of the web dev community, I've been working with "made-up" elements for the past year, in all of my production projects, without a polyfill, and I've had no unsolvable issues (yet), and no complaints. How? By relying on the standard behaviour of HTMLUnknownElement, the part of the W3C spec that covers the case of "made-up" elements. If a browser encounters an unrecognized HTML element, there is a well-defined and unambiguous way that it should be handled, and HTMLUnknownElement defines that behaviour, and you can build on top of that. HTMLUnknownElement also has to be powerful and correct enough to "not break the web" when encountering all the tags that are now obsolete, like the tag. It's not recommended that you use HTMLUnknownElement, but in theory and in practice, there's absolutely no harm in doing so, if you know what you're doing.

与大多数web开发社区发自内心的推荐(有时是尖叫)相反,我在过去的一年中一直在使用“虚构”元素,在我所有的生产项目中,没有一个polyfill,我也没有任何无法解决的问题(到目前为止),也没有任何抱怨。如何?通过依赖HTMLUnknownElement的标准行为,W3C规范中涵盖了“虚构”元素的部分。如果浏览器遇到一个未识别的HTML元素,那么应该以一种明确定义的、明确的方式来处理它,并且HTMLUnknownElement定义了这种行为,您可以在此基础上进行构建。HTMLUnknownElement还必须足够强大和正确,才能在遇到现在已经过时的所有标记时“不破坏web”,比如 标记。不建议您使用HTMLUnknownElement,但在理论上和实践中,如果您知道自己在做什么,那么这样做绝对没有害处。

So how does HTMLUnknownElement work? It is just an extension of the HTMLElement interface, which is the standard interface underlying all HTML elements. Unlike most other elements however, HTMLUnknownElement doesn't add any special behaviour — you get a raw element, unadorned with any special behaviour nor constraining rules about use. The HTMLDivElement interface works almost exactly the same way, extending HTMLElement and adding almost no additional behaviour. Put simply, making up your own element is almost identical to using a div or span.

那么htmlunnelement是如何工作的呢?它只是HTMLElement接口的一个扩展,它是所有HTML元素的标准接口。然而,与大多数其他元素不同,HTMLUnknownElement不添加任何特殊行为——您得到的是一个原始元素,没有任何特殊行为的修饰,也没有关于使用的约束规则。HTMLDivElement接口的工作方式几乎完全相同,扩展了HTMLElement并添加了几乎没有额外的行为。简单地说,创建自己的元素几乎等同于使用div或span。

What I like about "making-up" elements is the change of mindset. You should use or invent HTML elements based on several factors, ranging from how clear it makes the markup to read, to how the browser and screen readers and search engines parse your code, to how likely your code is to be "correct" by some objective measure. I sparingly use made-up elements, but I use in exactly the way Richard described, to make things more meaningful for the author of the HTML, not just meaningful to a computer service that extracts metadata. When used in a consistent way across a team, there can be a big benefit since made-up elements can concisely express what they're for.

我喜欢“化妆”元素的是心态的转变。您应该基于以下几个因素来使用或发明HTML元素,从如何清除标记到阅读,到浏览器和屏幕阅读器和搜索引擎如何解析代码,以及您的代码如何通过一些客观的度量来“正确”。我很少使用虚构的元素,但我使用的方式与Richard描述的完全一致,使HTML的作者更有意义,而不仅仅是对提取元数据的计算机服务有意义。当在团队中以一致的方式使用时,会有很大的好处,因为编造的元素可以简洁地表达它们的目的。

I particularly like using made-up elements to indicate when I will be using JS to define extra behaviour for an element. For instance, if I have an element that will have children added/removed by JS, I will use a made-up element as a clue that this element is subject to special behaviour. By the same token, I don't use a made-up element when a standard element will suffice. You will see live happily next to

in my code.

我特别喜欢使用虚构元素来指示何时使用JS定义元素的额外行为。例如,如果我有一个元素,它的子元素将被JS添加/删除,我将使用一个虚构的元素作为这个元素受特殊行为影响的线索。同样,当一个标准元素足够时,我不会使用一个虚构的元素。您将看到在我的代码中快乐地生活在

的旁边。

Now, about those pesky validators. Yes, using made-up elements isn't "valid" in the sense that it won't pass a "validator". But many commonly used features, patterns, and systems of modern HTML and JS development fail all the W3C validators. The validators aren't the law — the spec is. And the law isn't binding — the implementations in all the browsers are. The utility of validators has been dimishing for years as the flexability of HTML has been increasing, and as browsers have shifted in their relationship to the spec. Validators are great for people who aren't comfortable with HTML and need guidance. But if you're comfortable taking your guidance from the spec and from browser implementations, there's no reason to worry about being flunked by the validator. Certainly, if you follow many of the guidelines offered by Google, Apple, Microsoft, etc, for implementing any experimental features, you'll be working outside the bounds of the validator. This is absolutely an okay thing to do, so long as you're doing it deliberately and you know enough about what you're doing.

现在,关于那些讨厌的验证器。是的,使用虚构的元素不是“有效的”,因为它不会传递“验证器”。但是,现代HTML和JS开发中许多常用的特性、模式和系统都不能通过所有的W3C验证器。验证器不是法律——规范才是。而且法律没有绑定——所有浏览器的实现都是这样。随着HTML的可伸缩性不断增强,以及浏览器与规范的关系发生了变化,验证器的效用多年来一直在不断变化。但是,如果您愿意从规范和浏览器实现中获取指导,那么就没有理由担心验证器的失败。当然,如果您遵循谷歌、Apple、Microsoft等提供的许多指导方针,您将在验证器的范围之外工作。这绝对是一件可以做的事情,只要你有意识地去做,并且你知道你在做什么。

Therefore, if you're going to make up your own elements and rely on HTMLUnknownElement, don't just treat it like a wild west. You need to follow a few simple rules.

因此,如果你想要创建自己的元素并依赖于HTMLUnknownElement,不要把它当作一个蛮荒的西部。你需要遵循一些简单的规则。

  1. You have to use a hyphen in the name of your tag. If you do, you are guaranteed to never collide with a future edition of the HTML spec. So never say , always say .

    您必须在标记的名称中使用连字符。如果你这样做了,你将保证不会与HTML规范的未来版本发生冲突。所以永远不要说 <错误的> ,总是说

  2. Made-up elements can't be self-closing — you have to close them with a closing tag. You can't just say or , you have to say .

    合成元素不能是自闭的——您必须用关闭标记关闭它们。你不能只说 <错误的> 或 <仍然错误 />,你必须说

  3. You have to define a display property for your element in CSS, otherwise the rendering behaviour is undefined.

    您必须在CSS中为元素定义一个显示属性,否则呈现行为是未定义的。

That's about it. If you do these things, you should be good to use made-up elements in IE9 and up, relying on the safety net of the HTMLUnknownElement. For me, the benefits far, far outweigh the costs, so I've been using this pattern heavily. I run a SaaS site catering to major industrial corporations, and I've had no trouble or complaints thus far. If you have to support older versions of IE, it's wise to stay far away from any "2015" technology or their crude approximations, and stay safely within the well-trodden parts of the spec.

仅此而已。如果您做了这些事情,您应该很好地在IE9和up中使用虚构的元素,依赖于HTMLUnknownElement的安全网。对我来说,好处远远大于代价,所以我一直在大量使用这种模式。我经营着一个面向大型工业企业的SaaS网站,迄今为止,我没有遇到任何麻烦或投诉。如果您必须支持旧版本的IE,那么明智的做法是远离任何“2015”技术或它们的粗略近似,并在规范中被广泛使用的部分中安全使用。

So, in summary, the answer to your question is "yes, if you know what you're doing".

总之,你的问题的答案是“是的,如果你知道你在做什么”。

#3


9  

No. You will fail validation, you will get random issues cross browser and you WILL be eaten by said dinosaurs. CSS is the answer if you want your page to behave predictably.

不。你会失败验证,你会在浏览器中随机出现问题,你会被恐龙吃掉。如果你想让你的页面表现得可预测,CSS就是答案。

#4


8  

Yes We Can.

是的,我们可以。

There is a new spec going on about custom elements/tag - http://w3c.github.io/webcomponents/spec/custom/.

关于自定义元素/标记有一个新的规范——http://w3c.github.io/webcomponents/spec/custom/。

Only issue with this is you have to use js to register your new element

唯一的问题是您必须使用js来注册新元素

You can read more about this at

你可以阅读更多关于这个的信息。

https://developers.google.com/web/fundamentals/getting-started/primers/customelements

https://developers.google.com/web/fundamentals/getting-started/primers/customelements

#5


2  

Rule #1 of browser interoperability is: don't have errors. No matter how many browsers you test in, there are always browsers you can't test, for instance because they don't exist yet.
Also, unknown elements will be treated as , not

by most browsers currently.

浏览器互操作性的第一条规则是:不要有错误。无论您测试多少浏览器,总有一些浏览器您不能测试,例如,因为它们还不存在。此外,未知元素将被视为,而不是大多数浏览器当前的

If it's really source readability(*) you're after, you should look into XML+XSLT.
That way, you can use all the tag names you want, and make them behave in any way you like and you don't have to worry that will be a real element in some future version of HTML.

如果您真正想要的是源代码可读性(*),那么应该研究XML+XSLT。这样,您就可以使用所有您想要的标记名,并使它们以任何您喜欢的方式运行,并且您不必担心 将成为将来某个HTML版本中的一个真正的元素。

One good real world example is the element . If a website ever used and relied on the notion that this element would have no styles or special content by itself, they are in trouble now!

一个很好的现实世界示例是元素 。如果一个网站使用了 ,并且依赖于这个元素本身没有样式或特殊内容的概念,那么他们现在就有麻烦了!

(*) With XML+XSLT, the readability will be in the XML part, not the XSLT part, obviously.

(*)使用XML+XSLT,可读性将在XML部分,而不是XSLT部分。

#6


0  

It's bad practice and you should not do it. The browser renders it as div as fallback solution in most cases but it's not valid html and therefore never will pass a validity test.

这是不好的做法,你不应该这样做。在大多数情况下,浏览器将其作为div作为回退解决方案,但它不是有效的html,因此永远不会通过有效性测试。

#7


0  

In your example you are talking about , it's could be great but if html6 adds this tag for another element, your code won't be retrocompatible.

在您的示例中,您正在讨论 ,它可能很好,但是如果html6为另一个元素添加了这个标记,那么您的代码将不会具有可溯性。

#8


0  

Generally not recommendable, e.g. IE wont apply css-styles to unknown tags.

一般不推荐,例如:IE不会对未知标签应用css样式。

All other browsers render unknown tags as inline-Elements (which causes problems with nesting).

所有其他浏览器都将未知标记呈现为内联元素(这会导致嵌套问题)。

I recommend you the following article: http://diveintohtml5.info/ There is a section about unknown tags.

我向您推荐以下文章:http://diveintohtml5.info/有一个关于未知标签的章节。

#9


0  

In my case I use a lot of them into my Webkit-powered game GUI system, and everything works.

在我的例子中,我在我的webkit驱动的游戏GUI系统中使用了很多,并且一切正常。

#10


-1  

What's wrong with the judicious use of your stuff here -- >. It worked for scripts back around the time of the Cretaceous–Paleogene boundary. Dinosaurs ceased to be a problem around that time, that is apart from the flying, feathered variety.

明智地使用有什么错?它可以追溯到白垩纪-古第三纪边界时期。恐龙在那个时候不再是一个问题,除了会飞的、有羽毛的种类。


推荐阅读
  • POJ 2482 星空中的星星:利用线段树与扫描线算法解决
    在《POJ 2482 星空中的星星》问题中,通过运用线段树和扫描线算法,可以高效地解决星星在窗口内的计数问题。该方法不仅能够快速处理大规模数据,还能确保时间复杂度的最优性,适用于各种复杂的星空模拟场景。 ... [详细]
  • 尽管我们尽最大努力,任何软件开发过程中都难免会出现缺陷。为了更有效地提升对支持部门的协助与支撑,本文探讨了多种策略和最佳实践,旨在通过改进沟通、增强培训和支持流程来减少这些缺陷的影响,并提高整体服务质量和客户满意度。 ... [详细]
  • 为了确保iOS应用能够安全地访问网站数据,本文介绍了如何在Nginx服务器上轻松配置CertBot以实现SSL证书的自动化管理。通过这一过程,可以确保应用始终使用HTTPS协议,从而提升数据传输的安全性和可靠性。文章详细阐述了配置步骤和常见问题的解决方法,帮助读者快速上手并成功部署SSL证书。 ... [详细]
  • 投融资周报 | Circle 达成 4 亿美元融资协议,唯一艺术平台 A 轮融资超千万美元 ... [详细]
  • 深入解析Gradle中的Project核心组件
    在Gradle构建系统中,`Project` 是一个核心组件,扮演着至关重要的角色。通过使用 `./gradlew projects` 命令,可以清晰地列出当前项目结构中包含的所有子项目,这有助于开发者更好地理解和管理复杂的多模块项目。此外,`Project` 对象还提供了丰富的配置选项和生命周期管理功能,使得构建过程更加灵活高效。 ... [详细]
  • 在CentOS 7环境中安装配置Redis及使用Redis Desktop Manager连接时的注意事项与技巧
    在 CentOS 7 环境中安装和配置 Redis 时,需要注意一些关键步骤和最佳实践。本文详细介绍了从安装 Redis 到配置其基本参数的全过程,并提供了使用 Redis Desktop Manager 连接 Redis 服务器的技巧和注意事项。此外,还探讨了如何优化性能和确保数据安全,帮助用户在生产环境中高效地管理和使用 Redis。 ... [详细]
  • 本文是Java并发编程系列的开篇之作,将详细解析Java 1.5及以上版本中提供的并发工具。文章假设读者已经具备同步和易失性关键字的基本知识,重点介绍信号量机制的内部工作原理及其在实际开发中的应用。 ... [详细]
  • 在HTML布局中,即使将 `top: 0%` 和 `left: 0%` 设置为元素的定位属性,浏览器中仍然会出现空白填充。这个问题通常与默认的浏览器样式、盒模型或父元素的定位方式有关。为了消除这些空白,可以考虑重置浏览器的默认样式,确保父元素的定位方式正确,并检查是否有其他CSS规则影响了元素的位置。 ... [详细]
  • 本文详细解析了 Yii2 框架中视图和布局的各种函数,并综述了它们在实际开发中的应用场景。通过深入探讨每个函数的功能和用法,为开发者提供了全面的参考,帮助他们在项目中更高效地利用这些工具。 ... [详细]
  • 在对WordPress Duplicator插件0.4.4版本的安全评估中,发现其存在跨站脚本(XSS)攻击漏洞。此漏洞可能被利用进行恶意操作,建议用户及时更新至最新版本以确保系统安全。测试方法仅限于安全研究和教学目的,使用时需自行承担风险。漏洞编号:HTB23162。 ... [详细]
  • 本指南介绍了如何在ASP.NET Web应用程序中利用C#和JavaScript实现基于指纹识别的登录系统。通过集成指纹识别技术,用户无需输入传统的登录ID即可完成身份验证,从而提升用户体验和安全性。我们将详细探讨如何配置和部署这一功能,确保系统的稳定性和可靠性。 ... [详细]
  • Python 伦理黑客技术:深入探讨后门攻击(第三部分)
    在《Python 伦理黑客技术:深入探讨后门攻击(第三部分)》中,作者详细分析了后门攻击中的Socket问题。由于TCP协议基于流,难以确定消息批次的结束点,这给后门攻击的实现带来了挑战。为了解决这一问题,文章提出了一系列有效的技术方案,包括使用特定的分隔符和长度前缀,以确保数据包的准确传输和解析。这些方法不仅提高了攻击的隐蔽性和可靠性,还为安全研究人员提供了宝贵的参考。 ... [详细]
  • Keepalived 提供了多种强大且灵活的后端健康检查机制,包括 HTTP_GET、SSL_GET、TCP_CHECK、SMTP_CHECK 和 MISC_CHECK 等多种检测方法。这些健康检查功能确保了高可用性环境中的服务稳定性和可靠性。通过合理配置这些检查方式,可以有效监测后端服务器的状态,及时发现并处理故障,从而提高系统的整体性能和可用性。 ... [详细]
  • Java环境中Selenium Chrome驱动在大规模Web应用扩展时的性能限制分析 ... [详细]
  • 本文全面解析了 gRPC 的基础知识与高级应用,从 helloworld.proto 文件入手,详细阐述了如何定义服务接口。例如,`Greeter` 服务中的 `SayHello` 方法,该方法在客户端和服务器端的消息交互中起到了关键作用。通过实例代码,读者可以深入了解 gRPC 的工作原理及其在实际项目中的应用。 ... [详细]
author-avatar
muc4093631
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有