javascript - Font Awesome 5中属性data-auto-a11y的效果

标签 javascript attributes font-awesome-5

如果我们想使用SVG+JS的渲染 Font Awesome 5 图标,然后切换 css/all.css进入js/all.js .

使用 <script>浏览器中的标签:

<script 
  src="https://use.fontawesome.com/releases/v5.12.1/js/all.js" 
  data-auto-a11y="true">
</script>

我的问题是 data-auto-a11y 的效果是什么?在 Font Awesome 5 中?

有必要使用吗?

最佳答案

深入研究 GitHub 上的 Font Awesome 代码,特别是 /js/fontawesome.js首先,data-auto-a11y 属性映射到属性 autoA11y。通过代码搜索,看起来这个属性控制是否设置了各种 aria- 属性,特别是如果 autoA11ytrue 并且有一个标题属性然后 aria-labelledby已设置,否则 aria-hidden已设置。

所以,如果您关心可访问性(我认为每个人都应该),那么它确实很重要。但是,我还要补充一点,在代码的其他行上,似乎表明将其设置为 true 是默认值。我建议您可能不需要它,但在这种情况下它肯定是无害的。

Caveat: I know nothing a priori about the internal workings of Font Awesome, this is just based on an analysis of the code.

Font Awesome 代码片段

默认设置 ( line 257 )

  var _default = {
    familyPrefix: DEFAULT_FAMILY_PREFIX,
    replacementClass: DEFAULT_REPLACEMENT_CLASS,
    autoReplaceSvg: true,
    autoAddCss: true,
    autoA11y: true,
    searchPseudoElements: false,
    observeMutations: true,
    mutateApproach: 'async',
    keepOriginalSource: true,
    measurePerformance: false,
    showMissingIcons: true
  };

autoA11y的使用。 (Line 1605)

  function attributesParser (node) {
    var extraAttributes = toArray(node.attributes).reduce(function (acc, attr) {
      if (acc.name !== 'class' && acc.name !== 'style') {
        acc[attr.name] = attr.value;
      }

      return acc;
    }, {});
    var title = node.getAttribute('title');

    if (config.autoA11y) {
      if (title) {
        extraAttributes['aria-labelledby'] = "".concat(config.replacementClass, "-title-").concat(nextUniqueId());
      } else {
        extraAttributes['aria-hidden'] = 'true';
        extraAttributes['focusable'] = 'false';
      }
    }

    return extraAttributes;
  }

line 1874

    if (config.autoA11y && !title) {
      extra.attributes['aria-hidden'] = 'true';
    }

最后是line 2291

      if (config.autoA11y) {
        if (title) {
          attributes['aria-labelledby'] = "".concat(config.replacementClass, "-title-").concat(nextUniqueId());
        } else {
          attributes['aria-hidden'] = 'true';
          attributes['focusable'] = 'false';
        }
      }

关于javascript - Font Awesome 5中属性data-auto-a11y的效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60632390/

相关文章:

jquery - FontAwesome 5 关于点击事件?

reactjs - React-Native 中的 Font Awesome、Unicode 和变量

javascript - 在 KnockoutJS 中将数据推送到数组时出现问题

ruby - 有什么办法可以延迟资源的属性解析到 "execute"阶段?

javascript - 将代码从 canvas 转换为 svg

ruby-on-rails - rails ActiveRecord : Automatically Alias/Append Suffix?

c# - 允许您限制对 DLL 的访问的属性是什么?

css - Font Awesome 5 - 为什么不显示比特币、facebook、twitter 等图标?

javascript - 什么是 Service Worker 控制台? & 它在 Chrome 浏览器中的什么位置?

javascript - 在 JavaScript 中拦截对象属性访问