Correct me if I'm mistaken, but AFAIK, unknown HTML tags in markup (i.e. tags not defined in the HTML spec, like say, 如果我搞错了,请纠正我,但是在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 我这样问的原因是,如果这些标记遵从 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开发人员的普遍看法可能发生了变化。 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 如果您希望在标记中更具语义表达性,您可以使用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. 最后,一个简短的回答:不,这是不好的做法,您不应该这样做,并且在以后的开发中可能会出现不可预见的问题。 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 与大多数web开发社区发自内心的推荐(有时是尖叫)相反,我在过去的一年中一直在使用“虚构”元素,在我所有的生产项目中,没有一个polyfill,我也没有任何无法解决的问题(到目前为止),也没有任何抱怨。如何?通过依赖HTMLUnknownElement的标准行为,W3C规范中涵盖了“虚构”元素的部分。如果浏览器遇到一个未识别的HTML元素,那么应该以一种明确定义的、明确的方式来处理它,并且HTMLUnknownElement定义了这种行为,您可以在此基础上进行构建。HTMLUnknownElement还必须足够强大和正确,才能在遇到现在已经过时的所有标记时“不破坏web”,比如
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 我特别喜欢使用虚构元素来指示何时使用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,不要把它当作一个蛮荒的西部。你需要遵循一些简单的规则。 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 您必须在标记的名称中使用连字符。如果你这样做了,你将保证不会与HTML规范的未来版本发生冲突。所以永远不要说 <错误的> ,总是说
Made-up elements can't be self-closing — you have to close them with a closing tag. You can't just say 合成元素不能是自闭的——您必须用关闭标记关闭它们。你不能只说 <错误的> 或 <仍然错误 />,你必须说
You have to define a 您必须在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". 总之,你的问题的答案是“是的,如果你知道你在做什么”。 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就是答案。 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 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. 浏览器互操作性的第一条规则是:不要有错误。无论您测试多少浏览器,总有一些浏览器您不能测试,例如,因为它们还不存在。此外,未知元素将被视为,而不是大多数浏览器当前的 If it's really source readability(*) you're after, you should look into XML+XSLT. 如果您真正想要的是源代码可读性(*),那么应该研究XML+XSLT。这样,您就可以使用所有您想要的标记名,并使它们以任何您喜欢的方式运行,并且您不必担心
One good real world example is the element 一个很好的现实世界示例是元素
(*) With XML+XSLT, the readability will be in the XML part, not the XSLT part, obviously. (*)使用XML+XSLT,可读性将在XML部分,而不是XSLT部分。 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,因此永远不会通过有效性测试。 0 In your example you are talking about 在您的示例中,您正在讨论
0 Generally not recommendable, e.g. IE wont apply css-styles to unknown tags. 一般不推荐,例如:IE不会对未知标签应用css样式。 All other browsers render unknown tags as 所有其他浏览器都将未知标记呈现为内联元素(这会导致嵌套问题)。 I recommend you the following article: http://diveintohtml5.info/ There is a section about unknown tags. 我向您推荐以下文章:http://diveintohtml5.info/有一个关于未知标签的章节。 0 In my case I use a lot of them into my Webkit-powered game GUI system, and everything works. 在我的例子中,我在我的webkit驱动的游戏GUI系统中使用了很多,并且一切正常。 -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. 明智地使用有什么错?它可以追溯到白垩纪-古第三纪边界时期。恐龙在那个时候不再是一个问题,除了会飞的、有羽毛的种类。
) will eventually be treated as a regular .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.
EDIT
10 个解决方案
#1
div
s which need to be appended id
s or class
es.#2
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.
live happily next to
, always say
.
or
, you have to say
.display
property for your element in CSS, otherwise the rendering behaviour is undefined.#3
#4
#5
Also, unknown elements will be treated as , not
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.. 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!
#6
#7
, it's could be great but if html6 adds this tag for another element, your code won't be retrocompatible. #8
inline
-Elements (which causes problems with nesting).#9
#10
如何更有效地提升对支持部门的协助与支撑? - Enhancing Support for the Support Department: Strategies and Best Practices
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有