本文介绍了jQuery通过文本查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我,是否可以根据内容而不是 id 来查找元素?

我试图找到没有不同的类或id的元素。 (然后我需要找到那个元素的父元素)。 api.jquery.com/contains-selector/rel =noreferrer> :包含 选择器根据内容获取元素。

 < div>这是一个测试< / div> 
< div>另一个Div< / div>

'('div:contains(test)')。css('background-color','red');


Can anyone tell me if it's possible to find an element based on its content rather than by an id or class?

I am attempting to find elements that don't have distinct classes or id's. (Then I then need to find that element's parent.)

解决方案

You can use the :contains selector to get elements based on their content.

<div>This is a test</div>
<div>Another Div</div>

$('div:contains("test")').css('background-color', 'red');

Demo here

这篇关于jQuery通过文本查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:02
查看更多