简体   繁体   English

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

[英]getting Arial Label value from list of buttons

So we have various buttons which looks like this 所以我们有各种看起来像这样的按钮

  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> 

But this just returns an empty string. 但这只会返回一个空字符串。

[Question] So I have two questions, Why does it returns empty string and how can I grab the value of aria-label here? [问题]我有两个问题,为什么它返回空字符串,如何在这里获取aria-label的值?

document.querySelectorAll('button[aria-label]')[0] is giving you the button element. document.querySelectorAll('button[aria-label]')[0]为您提供了button元素。

So you're seeing the .value of the button element. 因此,您将看到button元素的.value If you want to see Connect then you can access the .innerText . 如果要查看“ Connect则可以访问.innerText

.getAttribute('aria-label') will give you the value of your aria label. .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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM