使用未知的HTML标签可以吗? [英] Is it OK to use unknown HTML tags?

查看:178
本文介绍了使用未知的HTML标签可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我错了,纠正我,但AFAIK,标记中的未知HTML标记(即HTML标记中未定义的标记,如< foobar> )将最终在HTML 5浏览器环境中作为常规< div> 处理。

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

我在想:这种做法有多可支持?我的意思是,如果我在标记中使用未知的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?

我问的原因是,如果这些标签推迟到< div> ,我可能会用更多的语义方式使用这些标签,比如分配类名确定模块。 拥有看看这篇文章,例如, .media class 。现在,如果不是写这个CSS来定位 .media ,我将它定位为< media> ,而不是?在我看来,这使得标记更具可读性和可维护性,但我确实承认它不是正确的HTML。

The reason I ask is that if these tags defer to <div>, 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 <media> instead? In my opinion, that makes the markup much more readable and maintainable, but I do acknowledge that it's not "correct" HTML.

只要透明,我就可以找到

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.

推荐答案

您应该始终按照其各自规范中的定义处理HTML。 定义新标签是一种极端的方法。它可能会通过浏览器检查,因为它实现了各种失败者,但不能保证这一点。充其量,你正在进入未定义的行为之地。更何况你会失败验证测试,但你似乎意识到这一点。

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.

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

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.

最后,一个简短的回答:不,这是不好的习惯,不应该这样做,并且在开发过程中可能会出现无法预料的问题。

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.

这篇关于使用未知的HTML标签可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆