通过类名和文本查找元素的选择器是什么?例如:通过类名和文本查找元素的选择器
<div class="a1">some text</div>
<div class="a1">some other text</div>
<div class="a2">some text</div>
假设我只关心class“a1”和“some text”的div元素。
由于提前, 阿尔瓦罗
通过类名和文本查找元素的选择器是什么?例如:通过类名和文本查找元素的选择器
<div class="a1">some text</div>
<div class="a1">some other text</div>
<div class="a2">some text</div>
假设我只关心class“a1”和“some text”的div元素。
由于提前, 阿尔瓦罗
您可以使用:contains()选择这样的:
$('.a1:contains("some text")');
我很新的堆栈,但我希望这是你在问什么。
if (document.getElementsByClassName("a1").text = "some text") {...}
可以使用document.querySelectorAll()
与参数选择"[class='a1']"
,数组文本和扩散元素投NodeList
到Array
,Array.prototype.find()
检查和元素的变量或字符串文字匹配.textContent
财产。
[...document.querySelectorAll("[class=a1]")]
.find(({textContent}) => textContent === "some text")
.style.color = "green";
<div class="a1">some text</div>
<div class="a1">some other text</div>
<div class="a2">some text</div>
整洁......我不知道这件事。 –