使用弹出框 API,以 `<div role="button">` 作为调用元素。

huangapple go评论48阅读模式
英文:

Use popover api with `<div role="button">` as invoking element

问题

使用弹出窗口API时,我想要打开一个带有&lt;div role=&quot;button&quot;&gt;的弹出窗口,但似乎不起作用。以下是尝试此操作的代码片段:

<div role="button" popovertarget="mypopover" style="border: 1px solid black; cursor: pointer;">Broken toggle</div>
<button popovertarget="mypopover">Happy toggle</button>
    
<div id="mypopover" popover>弹出窗口内容</div>

只有当&lt;button&gt;是调用元素时,它似乎起作用。

是否有人知道调用元素是否可以是除了&lt;button&gt;之外的其他东西?

英文:

When using the popover api I would like to open a popover with &lt;div role=&quot;button&quot;&gt; but this doesn't seem to work. Here is a snippet that attempts this.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div role=&quot;button&quot; popovertarget=&quot;mypopover&quot; style=&quot;border: 1px solid black; cursor: pointer;&quot;&gt;Broken toggle&lt;/div&gt;

<button popovertarget="mypopover">Happy toggle</button>

<div id="mypopover" popover>Popover content</div>

<!-- end snippet -->

It only seems to work if a &lt;button&gt; is the invoking element.

Does someone know if the invoking element can be something else then a &lt;button&gt;?

答案1

得分: 2

在 HTML 中选择使用哪个标签不仅仅是语义的问题。许多元素也具有内置行为,在 JavaScript 中也可以访问该元素。尽管可以通过像 role 这样的属性来部分控制元素的语义,但这并不会改变它们的实际行为。

在 Popover API 的情况下,有一个限制,只有 &lt;button&gt;&lt;input&gt; 元素才能具有 popovertarget 属性。MDN 对 Popover API 的页面有相关文档。

通常情况下,可以编写带有无效标签的 HTML,但浏览器会将其忽略。类似于如果您尝试在 &lt;p&gt; 标签上添加 href 属性,它不会变成链接。

当您使用 role 更改元素的语义而不是直接使用适当的元素时,应该非常谨慎和深思熟虑。在这个例子中,当您使用 &lt;div&gt; 代替应该使用 &lt;button&gt; 时,会丧失一些内置在按钮中的功能,比如能够接收键盘焦点,以及将某些按键视为“点击”事件。对于那些使用键盘而不是鼠标等指针设备的人来说,这些都是非常重要的无障碍功能,例如可能是因为他们是盲人或有运动障碍。

可以通过给元素添加 tabindex=&quot;0&quot; 并添加事件侦听器来复制这些功能,但更容易的方法是直接使用 &lt;button&gt;。如果问题只是样式方面的,这在我看来通常是人们避免使用 &lt;button&gt; 元素的情况,实际上应该使用它的情况,最好通过 CSS 解决。

如果您也是这种情况,那么您可能会对了解 unset CSS 属性感兴趣,它现在已经得到了所有现代浏览器的支持,可以使将“干净状态”的样式应用于按钮变得更容易。

英文:

Choosing which tag to use in HTML isn't just a question of semantics. Many elements also have built-in behaviour, which is also exposed on the element in JavaScript. Though it's possible to have some control of the semantics of an element through attributes like role, this doesn't change their actual behaviour.

In the case of the Popover API, there is a limitation that only &lt;button&gt; and &lt;input&gt; elements are able to have a popovertarget attribute. MDN's page on the Popover API documents this.

As usual, it's possible to author HTML with invalid tags, but the browser will just ignore these. Similar to if you try to put an href attribute on a &lt;p&gt; tag, it won't turn into a link.

You should think very carefully and very deliberately when using role to change the semantics of an element instead of just using an appropriate element in the first place. In this example of using a &lt;div&gt; where you should use a &lt;button&gt;, you lose several pieces of functionality that are built into buttons such as being able to receive keyboard focus, and treating certain key presses as "click" events. These are very important accessibility features for people who use a keyboard instead of a pointer device like a mouse, for example because they may be blind or have a motor disability.

It's possible to replicate these through methods such as giving the element tabindex=&quot;0&quot; and adding event listeners to handle those keyboard events, but it's much easier to just use a &lt;button&gt;. If the problem is just one of styling, which in my experience is often the case when people avoid using a &lt;button&gt; element for something that really should use one, it's best to resolve that through CSS.

If this is the case for you too, then you may be interested to learn about the unset CSS property, which is supported by all modern browsers now and can make it much easier to apply "clean slate" styling to a button.

huangapple
  • 本文由 发表于 2023年7月14日 03:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定