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

问题描述

我正在尝试在元素为myClass1或myClass2的元素内查找文本.

I'm trying to find text inside an element whose class is either myClass1 OR myClass2.

var myText = $(this).find('.myClass1:first').text();

这很好用,但是我不确定是否/如何检查2个类之一(我提到的这2个类中,我的元素将只有一个类).

This works fine but I am unsure if/how I can check for one of 2 classes (my element will only have one class out of these 2 I mentioned).

感谢您的帮助!

推荐答案

如果要找到第一个(但只有一个),请使用

If you want the first one found (but only one) use

var myText = $(this).find('.myClass1,.myClass2').eq(0).text();

如果您想要每种中的第一种(两个结果),请查看.

If you want the first of each kind (two results) then look at the answer provided by @jelbourn.

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

08-04 05:12
查看更多