我可以在queryselector()中为CONTAINS使用属性选择器吗? [英] Can I use an attribute selector for CONTAINS in queryselector()?
问题描述
我想根据href属性的子字符串选择一个元素。
I would like to select an element based on a substring of href attribute.
我最近发布了一个问题,我收到了一个基于开头的答案:
I posted a question recently where I received an answer based on "starts with":
document.querySelector('#utility-menu a[href^="/email_check?lang="').textContent.trim();
但实际上,页面到页面的唯一常量是lang = href属性中的
。所以 / email_check?
是特定于页面的,所以我不能在al页面上使用这个变量。
But actually, the only constant from page to page will be "lang="
within the href attribute. So /email_check?
is page specific so I cannot use this variable on al pages.
是否可以修改我的选择器返回任何< a>
元素的textContent,其中包含子字符串lang =
?
Is it possible to modify my selector to return the textContent of any <a>
element with the substring "lang="
within it?
推荐答案
所有选择器都记录在 W3C选择器概述。
您需要的是
#utility-menu a[href*="lang="]
E[foo*="bar"]
一个 E
元素,其 foo
属性值包含子串 bar
。
An E
element whose foo
attribute value contains the substring bar
.
这篇关于我可以在queryselector()中为CONTAINS使用属性选择器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!