【问题标题】:BEM. How to deal with label for/id?边界元法。如何处理标签 for/id?
【发布时间】:2018-11-30 10:18:32
【问题描述】:

据我所知,BEM 根本不使用元素 id。但是在这种情况下如何处理结合输入的标签 for/id 呢?如果我不使用 id,使用屏幕阅读器的人不会知道这个标签是针对那个输入的。我说的对吗?

【问题讨论】:

    标签: input label accessibility bem


    【解决方案1】:

    BEM 建议避免 id 用于 css 选择器。对于 A11y 和可用性,使用 id 是完全有效的。

    <form class="form" method="post" action="">
        <label for="email" class="form__label">Email</label>
        <input type="email" id="email" class="form__control"/>
    </form>
    

    在上面的示例中,我们将输入设置为带有 form__control 类的 form 块的元素。

    此外,您不能使用没有id 的 aria 属性作为指向描述性元素的指针。

    <div role="application" aria-labelledby="calendar" aria-describedby="info">
        <h1 id="calendar">Calendar</h1>
        <p id="info">
            This calendar shows the game schedule for the Boston Red Sox.
        </p>
        <div role="grid">
            ...
        </div>
    </div>
    

    示例来自:https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute

    【讨论】:

    • 你的意思是 BEM 避免 id 仅用于选择器,但可以在标记中使用它?
    • @andrey.shedko 是的,当然 :) 如果不使用 id 属性,您将无法制作可访问的应用程序。甚至创建 BEM 的人也在使用 id:yandex.com 检查他们的代码 :)
    猜你喜欢
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多