自定义元素(htmlelement)允许作为列表的孩子(< ul> and&&< ol>)?

发布于 2025-01-26 02:58:35 字数 323 浏览 5 评论 0原文

我认为这是不允许的:

<ul>
  <my-li>
    #shadow-root
    <li>
      <span>hello!</span>
    </li>
  </my-li>
</ul>

但是我们该怎么做才能不打破语义?我知道,htmlelement以外的其他元素将存在,但是Safari并非如此。获得适当的HTML语义的最佳解决方案是什么?

I figure this is not allowed:

<ul>
  <my-li>
    #shadow-root
    <li>
      <span>hello!</span>
    </li>
  </my-li>
</ul>

But what can we do to not break semantics? I know that elements other than HTMLElement would exist, but Safari is not okay with this being the case. What would be the best solution to achieve a proper HTML semantic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2025-02-02 02:58:35

当您构造自定义元素时,您可以告诉它以这样的方式扩展现有元素:

class MyLI extends HTMLLIElement {
  constructor() {
    super()
    ...
  }
}

customElements.define('my-li', MyLI, { extends: 'li' })

要使用它,您仍然会使用常规,语义,语法上正确的列表项目元素,但是您告诉浏览器,它是一个实例带有的自定义元素是属性。

<ul>
  <li is="my-li">
    #shadow-root
    <span>hello!</span>
  </li>
</ul>

Custom elements can customize existing elements. If you're wanting to create a custom list item element, you should probably extend the existing one. This will allow you to validly place it as a child of an unordered list element.

When you're constructing a custom element, you can tell it to extend an existing element like so:

class MyLI extends HTMLLIElement {
  constructor() {
    super()
    ...
  }
}

customElements.define('my-li', MyLI, { extends: 'li' })

To use it, you would still use a regular, semantic, syntactically correct list item element, but you tell the browser it's an instance of a custom element with the is attribute.

<ul>
  <li is="my-li">
    #shadow-root
    <span>hello!</span>
  </li>
</ul>
时光暖心i 2025-02-02 02:58:35

如果您正在寻求验证HTML元素的语义使用,则可以执行在这里

If you are looking for validating on semantic use of HTML elements, then you can do that here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文