简体   繁体   English

我可以定义自己的自定义HTML元素以用于css和js吗?

[英]Can I define my own custom HTML Elements for use with css and js?

I've been reading all about these hot new custom HTML elements that you can make with today's browsers; 我一直在阅读所有关于这些热门的新自定义HTML元素 ,你可以使用今天的浏览器; It seems pretty interesting, to be honest. 说实话,这似乎很有趣。 I've seen that the camp is out on this one, though; 不过,我已经看到营地在这个营地了; There's a lot of debate over whether to use them or not. 关于是否使用它们存在很多争论。

But that's not my question! 但那不是我的问题!

I've seen that in order to use custom HTML elements, you have to declare the element with javascript first; 我已经看到,为了使用自定义HTML元素,你必须先用javascript声明元素; but I'm not finding this to be the case. 但我发现情况并非如此。

I simply ran an HTML document with the following code; 我只是使用以下代码运行HTML文档;

<div>
   <header-label>Text</header-label>
</div>

And it worked just fine in Chrome ( 40 ), FireFox ( 40 ), Internet Explorer 11, and Edge. 它在Chrome( 40 ),FireFox( 40 ),Internet Explorer 11和Edge中运行良好。

So I'm more confused than before; 所以我比以前更困惑; What is going on? 到底是怎么回事? Do I need to declare them, or not? 我是否需要申报? Even CSS worked against it, as did jQuery selectors, angular directives; 即使是CSS ,jQuery选择器,角度指令也是如此; I couldn't find anything that led me to believe this was anything but a normal HTML element. 我找不到任何让我相信这只是普通HTML元素的东西。

A) User-defined tags A)用户定义的标签

You can use user-defined tags anywhere. 您可以在任何地方使用用户定义的标签。

If you want to put <header-label> that's up to you. 如果你想把<header-label>放在你面前。 There are also custom HTML5 attributes which are things like 还有自定义的HTML5属性

<input name="q" placeholder="Go to a Website">

or 要么

<input type="email">

I investigated further and read those blog post comments and learned about... 我进一步调查并阅读了博客文章的评论并了解了...

B) HTML5 Custom Elements: B)HTML5自定义元素:

Per http://www.html5rocks.com/en/tutorials/webcomponents/customelements/ 根据http://www.html5rocks.com/en/tutorials/webcomponents/customelements/

These let you do things like 这些让你做的事情

<hangout-module>
  <hangout-chat from="Paul, Addy">
    <hangout-discussion>
      <hangout-message from="Paul" profile="profile.png"
          profile="118075919496626375791" datetime="2013-07-17T12:02">
        <p>Feelin' this Web Components thing.</p>
        <p>Heard of it?</p>
      </hangout-message>
    </hangout-discussion>
  </hangout-chat>
  <hangout-chat>...</hangout-chat>
</hangout-module>

These would otherwise be ignored (though the content and valid tags within them will still render without issue), however you can add these elements as valid DOM elements with: 否则这些将被忽略(尽管它们中的内容和有效标签仍将呈现而没有问题),但是您可以将这些元素添加为有效的DOM元素:

var XFoo = document.registerElement('x-foo');
document.body.appendChild(new XFoo());

You can also base a user-defined element on an existing element to extend it, eg 您还可以在现有元素上基于用户定义的元素来扩展它,例如

var MegaButton = document.registerElement('mega-button', {
  prototype: Object.create(HTMLButtonElement.prototype),
  extends: 'button'
});

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

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