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

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

jQuery Selectors: Select element with specific class and 'title' attribute

jqueryclassjquery-selectorsattributestitle

提问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 currentStatusis an array, you could get all statuslightelements with a title that contains any of the currentStatusvalues 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 currentStatusis only one item you would use this selector:

或者,如果您的标题有多个状态 ( title="status1 status2 status3"),并且currentStatus只有一个项目,您将使用此选择器:

$(".statuslight[title~='" + currentStatus + "']");