jQuery 选择器:选择具有特定类和“标题”属性的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150783/
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
jQuery Selectors: Select element with specific class and 'title' attribute
提问by MegaMatt
This should be an easy one, but I can't find a good example anywhere. I'm trying to select a particular element on my page that has a class of 'statuslight' and a title attribute of one of three options. The option (it will depend) is stored in a variable called 'currentStatus'. I've seen some examples, and I'm guessing this is the right track, but I need to know for sure:
这应该很简单,但我在任何地方都找不到好的例子。我试图在我的页面上选择一个特定元素,该元素具有“statuslight”类和三个选项之一的标题属性。该选项(取决于)存储在一个名为“currentStatus”的变量中。我看过一些例子,我猜这是正确的轨道,但我需要确定:
$(".statuslight[title]='" + currentStatus + "'");
回答by Joel
Very close:
很接近:
$(".statuslight[title='" + currentStatus + "']");
Supposing your currentStatus
is an array, you could get all statuslight
elements with a title that contains any of the currentStatus
values with:
假设您currentStatus
是一个数组,您可以获得statuslight
标题包含以下任何currentStatus
值的所有元素:
$(".statuslight[title='" + currentStatus[0] + "'], .statuslight[title='" + currentStatus[1] + "'], .statuslight[title='" + currentStatus[2] + "']");
Alternatively, if your title has multiple statuses (title="status1 status2 status3"
), and currentStatus
is only one item you would use this selector:
或者,如果您的标题有多个状态 ( title="status1 status2 status3"
),并且currentStatus
只有一个项目,您将使用此选择器:
$(".statuslight[title~='" + currentStatus + "']");