jQuery:按文本查找元素 [英] jQuery: find element by text

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

问题描述

有人可以告诉我是否有可能根据元素的内容而不是 id class 来查找元素吗?

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

我正在尝试查找没有不同类或ID的元素. (然后,我需要找到该元素的父元素.)

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.)

推荐答案

您可以使用 :contains 选择器可根据其内容获取元素.

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

在此处演示

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

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆