基于数据绑定名称的document.querySelector

时间:2019-01-15 21:16:28

标签: javascript

<span data-automation="name" data-bind="text: ContactInformation.PersonName.fullName">John Smith</span>

我有一个Chrome扩展程序,可从页面中抓取某些元素值。传统上,它们具有我可以使用的ID或类名,但是在这里,我仅具有数据绑定或数据自动化属性。

是否可以在此处使用document.querySelector返回此范围的innerText?

1 个答案:

答案 0 :(得分:1)

您可以使用Attribute selectors

 var x = document.querySelector("[data-bind='text: ContactInformation.PersonName.fullName']").textContent;
 
 console.log(x);
<span data-automation="name" data-bind="text: ContactInformation.PersonName.fullName">John Smith</span>