试图根据文本内容查找元素

时间:2018-01-18 10:31:07

标签: javascript xpath

我正在尝试根据里面的文字找到一个元素。例如,要找到此链接:

<a href="#">Foobar</a>

使用下面的代码,nullconsole.log(matchingElement)两种情况下都会var xpath = "a[text()='Foobar']"; var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; console.log(matchingElement); xpath = "a[contains(text(),'Foobar')]"; matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; console.log(matchingElement);

<a href="#">Foobar</a>
<a target="_blank" href="{% url "admin:auth_user_changelist" %}">Utilisateurs</a>

0 个答案:

没有答案