HTML5:为什么不使用我自己的任意标签名称? [英] HTML5: Why not use my own arbitrary tag names?

查看:98
本文介绍了HTML5:为什么不使用我自己的任意标签名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在HTML5中组合标签名称是完全合法的,并且它们可以像CSS样式和嵌套一样正常工作。

As far as I know, it is perfectly legal to make up tag names in HTML5, and they work like normal with CSS styling and nesting and all.

当然,我的任意标签名称对浏览器没有任何影响,浏览器不理解它们,但它使我的代码更易读,这使得它更容易维护。

Of course my arbitrary tag names will make no difference to the browser, which does not understand them, but it goes very far in making my code more readable, which makes it easier to maintain.

那为什么我不应该在我的页面上使用任意标签名称?它会影响SEO吗?它会破坏什么吗?

So why should I NOT use arbitrary tag names on my pages? Will it affect SEO? Will it break anything?

重要编辑:与 http://ejohn.org/blog/html5-shiv/

推荐答案

更新



原始答案相当陈旧,并且是在Web组件存在之前创建的(尽管它们自2011年开始讨论)。自定义元素的规则已经发生了很大的变化。 W3C现在有自定义元素的完整标准


自定义元素为作者构建自己的全功能DOM元素提供了一种方法。虽然作者总是可以在他们的文档中使用非标准元素,但事后通过脚本或类似方式添加特定于应用程序的行为,这些元素在历史上一直是不符合的并且不是很有用。通过定义自定义元素,作者可以通知解析器如何正确构造元素以及该类的元素应如何对更改做出反应。

Custom elements provide a way for authors to build their own fully-featured DOM elements. Although authors could always use non-standard elements in their documents, with application-specific behaviour added after the fact by scripting or similar, such elements have historically been non-conforming and not very functional. By defining a custom element, authors can inform the parser how to properly construct an element and how elements of that class should react to changes.

换句话说,W3C现在可以使用自定义元素(或任意标记名称,因为问题调用它们)。然而,围绕它们和它们的使用有相当多的规则,所以你应该参考链接的文档。

In other words, the W3C is now fine with you using custom elements (or arbitrary tag names as the question calls them). However, there are quite a few rules surrounding them and their use so you should refer to the linked documentation.


  1. 旧版浏览器窒息了他们无法识别的标签。没有任何东西可以保证您的自定义标签在任何浏览器中都能像您期望的那样工作

  2. 标签没有语义含义,因为这个含义只存在于spec中商定的标签中。这是否影响SEO是未知的,但解析HTML的任何内容如果遇到无法识别的标签(包括屏幕阅读器或网络抓取器)可能会遇到麻烦

  3. 您可能无法获得如果您的标签嵌套不正确,您需要从浏览器获得帮助。
  4. 同样,某些浏览器可能会使渲染搞砸并且彼此不同。例如,在Chrome中将< p>< ul>< / ul>< / p> 解析为< p><< ; / p为H.< UL>< / UL> 。不能保证你的标签如何被处理,因为他们没有同意的内容模型。
  5. W3C 不希望你这样做。

  1. Older browsers choke on tags they don't recognize. There is nothing that guarantees that your custom tags will work as you expect in any browser
  2. There is no semantic meaning applied to your tags since this meaning only exists in the agreed upon tags in the spec. Whether or not this affects SEO is unknown, but anything that parses the HTML may have trouble if it runs into a tag it doesn't recognize including screen readers or web scrapers
  3. You may not get the help you need from the browser if your tags are improperly nested
  4. In the same vein, some browsers may screw up rendering and be different from one another. For example, <p><ul></ul></p> is parsed in Chrome as <p></p><ul></ul>. There is no guarantee how your tags will be treated since they have no agreed upon content model
  5. The W3C doesn't want you to do it.

作者不得使用本规范或其他适用规范不允许的元素,属性或属性值,因为这样做会使其更难语言将在未来扩展。

Authors must not use elements, attributes, or attribute values that are not permitted by this specification or other applicable specifications, as doing so makes it significantly harder for the language to be extended in the future.


如果你真的需要使用自定义标签为此,您可以使用某种类型的模板语言(如XSLT),并创建一个将其转换为有效HTML的样式表。

If you really need to use custom tags for this purpose, you can use a template language of some sort such as XSLT and create a stylesheet that transforms it into valid HTML

这篇关于HTML5:为什么不使用我自己的任意标签名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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