从按钮列表中获取Arial标签值

时间:2018-06-29 19:11:39

标签: javascript

所以我们有各种看起来像这样的按钮

  var val =  document.querySelectorAll('button[aria-label]')[0].value;
  console.log(val);
<button aria-label="Connect with Trijay Sharda" data-control-name="srp_profile_actions" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-6704="6704" data-is-animating-click="true">Connect</button>

<button aria-label="Connect with Vibhor Jain" data-control-name="srp_profile_actions" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-7497="7497">Connect</button>

但这只会返回一个空字符串。

[问题] 所以我有两个问题,为什么它返回空字符串,如何在这里获取aria-label的值?

1 个答案:

答案 0 :(得分:2)

document.querySelectorAll('button[aria-label]')[0]为您提供了按钮元素。

因此,您看到的是按钮元素的.value。如果您想查看Connect,则可以访问.innerText

.getAttribute('aria-label')将为您提供aria标签的价值。

  var button =  document.querySelectorAll('button[aria-label]')[0];
  console.log(button.getAttribute('aria-label')); // Connect with Trijay Sharda
  console.log(button.innerText); // Connect
<button aria-label="Connect with Trijay Sharda" data-control-name="srp_profile_actions" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-6704="6704" data-is-animating-click="true">Connect</button>

<button aria-label="Connect with Vibhor Jain" data-control-name="srp_profile_actions" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-7497="7497">Connect</button>