jQuery 如何使用jquery检查TR是否包含具有特定CSS类的TD?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10360450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:11:07  来源:igfitidea点击:

How to check if a TR contains a TD with a specific CSS class with jquery?

jquerycssjquery-selectors

提问by Rolando

I know you can use .findto find td:contains('text'), but if I have a tr with say, 3 td's, and one of the td's mighthave class="specialclass someotherclass"(may potentially have other classes in addition to special class), how do I use jquery to check if a TR contains a TD of specialclass?

我知道你可以.find用来 find td:contains('text'),但是如果我有一个 tr 说,3 个 td,并且其中一个 td可能class="specialclass someotherclass"(除了特殊类之外可能还有其他类),我如何使用 jquery 来检查一个 TR包含specialclass?

回答by BoltClock

To select any trthat has a td.specialclass:

要选择任何tr具有td.specialclass

$('tr:has(td.specialclass)')

Or if you have a tr(represented by this) and you simply want to check if it has such a td:

或者,如果您有一个tr(由 表示this)并且您只想检查它是否有这样的td

if ($(this).find('td.specialclass').length)

回答by dtbarne

if ($("tr").has("td.specialclass").length > 0) {
    // has specialclass
}

or

或者

if ($("tr:has(td.specialclass)").length > 0) {
    // has specialclass
}