如何将aria-label属性添加到视图中的链接?

时间:2014-11-09 18:27:02

标签: ruby-on-rails ruby wai-aria

我尝试将aria-label属性添加到链接中以使其更易于访问。当我这样做时,它按预期工作:

<a href="/" class="site-name <%= is_active('home') %>" aria-label="<%= get_aria_label_current_page('home') %>">Version Postman</a>

但这并不是:

<%= link_to t('nav.projects'), projects_path, class: is_active('projects'), aria-label: get_aria_label_current_page('home') %>

我得到了一个意外的tLABEL&#34;语法错误。任何人都知道问题是什么?

感谢。

2 个答案:

答案 0 :(得分:12)

这是创建问题的标签上的破折号。试试这个:

<%= link_to t('nav.projects'), projects_path, class: is_active('projects'), 'aria-label' => get_aria_label_current_page('home') %>

<强>更新

现在你可以在ruby 2.2中做到:

'aria-label': get_aria_label_current_page('home')

答案 1 :(得分:3)

至少从Rails 5.2开始,它也有效:

<div class="content">
  <h2>My Header</h2>
  <div class="list-wrapper">
    <div class="list">
      <div class="list-item">My List Item 1</div>
      <div class="list-item">My List Item 2</div>
      <div class="list-item">My List Item 3</div>
      <div class="list-item">My List Item 4</div>
      <div class="list-item">My List Item 5</div>
      <div class="list-item">My List Item 6</div>
      <div class="list-item">My List Item 7</div>
      <div class="list-item">My List Item 8</div>
      <div class="list-item">My List Item 9</div>
      <div class="list-item">My List Item 10</div>
    </div>
  </div>
  <div class="add-new">
    <button>Add</button>
  </div>
</div>

这类似于<%= link_to t('nav.projects'), projects_path, class: is_active('projects'), aria: { label: get_aria_label_current_page('home') } %> 属性的工作方式,这很不错,因为您可以添加多个属性并将其分组。

这可能适用于早期版本,但我尚未检查。