html 如何根据可访问性需求定义自定义li元素?

gzszwxb4  于 2022-11-27  发布在  其他
关注(0)|答案(2)|浏览(144)

我想将我自己的列表项构建为自定义元素

<ul>
  <my-li><!-- Advanced stuff in shadow dom will be rendered here --></my-li>
</ul>

然而,在ul标记中,只允许使用以下元素(li、script、template)。
1.)是否在自定义元素上定义一个aria-role?

<ul>
  <my-li aria-role="listitem"><!-- Advanced stuff in shadow dom will be rendered here --></my-li>
</ul>

1.在影子世界里使用li标签?

<ul>
  <my-li><!-- Advanced stuff in shadow dom will be rendered within a li-tag here --></my-li>
</ul>

1.其他的

wz3gfoph

wz3gfoph1#

你需要做一些其他的事情,因为你的前两个都是无效的HTML。你有几个选择,从最好到最坏:
方案一:

<ul>
  <li>
    <your-custom-element/>
  </li>
</ul>

备选方案二:
在自定义元素的shadow DOM中管理整个<ul>,或者将<ul>作为它的根。
选项3,您应该避免。

<div role="list">
  <your-custom-element role="list-item"/>
</div>

The best would be my option 1. It works because you can put anything you like inside `<li>`.

You should avoid my option 3 if you can, for several reasons:

- Avoid using ARIA unless it's really necessary
- No ARIA is better than bad ARIA
- It's not very clear if specifying the role on a custom element will work as expected, or if you have to report it yourself to the top element of your shadow DOM. It depends on what the browser / accessibility tree pass to assistive tools, and, as far as I know, it isn't very well defined, since custom elements are pretty new
w51jfk4q

w51jfk4q2#

这是3)

<div role="list">
    <my-li role="listitem"></my-li>
</div>

它不能是1)或2),因为两者都违反HTML规范。
3)HTML规范和所有需求都得到满足。

相关问题