如何在jquery中获取包含'map'的类名?

时间:2023-02-04 20:36:34

Does anyone how can I get the class name with contain word 'map' with jquery? I'm using the following code to get the class name with map (eg. class="mapTest"), but it not work if the class contain 2 name (e.g class="abctest map test"). Does anyone know how can I fix it?

有没有人如何通过jquery获取包含单词'map'的类名?我正在使用以下代码来获取带有map的类名(例如,class =“mapTest”),但如果该类包含2个名称(例如class =“abctest map test”)则不起作用。有谁知道我该如何解决它?

     $(":input[class^=map]").each(function(index, element) {
        map_array.push(this.className);
    });

3 个解决方案

#1


7  

Have you tried the contains selector? It checks the class name to see if it contains a specified substring. For example:

你试过包含选择器吗?它检查类名以查看它是否包含指定的子字符串。例如:

$('input[class*="map"]');

See if that works.

看看是否有效。

More here: http://api.jquery.com/attribute-contains-selector/

更多信息:http://api.jquery.com/attribute-contains-selector/

#2


4  

You were close, you want *=, not ^=:

你很亲密,你想要* =,而不是^ =:

$(':input[class*="map"]').each(function(index, element) {
    map_array.push(this.className);
});

This is the "attribute contains" selector (jQuery docs, CSS spec).

这是“属性包含”选择器(jQuery docs,CSS规范)。


About the quotes: They're optional if the value you're searching for fits the definition of a CSS identifier (your map value does, for instance). They're required if you're matching something that doesn't fit that definition (like anything with a space in it, or starting with an unescaped digit).

关于引号:如果您要搜索的值符合CSS标识符的定义(例如,您的地图值),则它们是可选的。如果您匹配的东西不符合该定义(例如任何带有空格的东西,或者以未转义的数字开头),则需要它们。

Since quotes are never wrong, best to always use them, but you don't have to if you know you're looking for a simple thing like map.

因为引号永远不会出错,所以最好总是使用它们,但如果你知道你正在寻找一个像地图这样简单的东西,你就不必这么做了。

#3


0  

Try using ~= instead. It checks for if it contains the text in cases of multiple classes.

尝试使用〜=代替。它会在多个类的情况下检查它是否包含文本。

$(":input[class~=map]").each(function(index, element) {
        map_array.push(this.className);
    });

Edit: Sorry, I should clarify. This is for matching when there are multiple classes. (If you had class="map blahmap") it would match map only, where-as *= would match both - This is more useful when you're trying to check if something has a specific class name amongst multiple classes)

编辑:对不起,我应该澄清一下。这是为了在有多个类时进行匹配。 (如果你有class =“map blahmap”)它只会匹配map,where-as * =会匹配两者 - 当你试图检查某些东西是否有多个类中的特定类名时,这会更有用)

#1


7  

Have you tried the contains selector? It checks the class name to see if it contains a specified substring. For example:

你试过包含选择器吗?它检查类名以查看它是否包含指定的子字符串。例如:

$('input[class*="map"]');

See if that works.

看看是否有效。

More here: http://api.jquery.com/attribute-contains-selector/

更多信息:http://api.jquery.com/attribute-contains-selector/

#2


4  

You were close, you want *=, not ^=:

你很亲密,你想要* =,而不是^ =:

$(':input[class*="map"]').each(function(index, element) {
    map_array.push(this.className);
});

This is the "attribute contains" selector (jQuery docs, CSS spec).

这是“属性包含”选择器(jQuery docs,CSS规范)。


About the quotes: They're optional if the value you're searching for fits the definition of a CSS identifier (your map value does, for instance). They're required if you're matching something that doesn't fit that definition (like anything with a space in it, or starting with an unescaped digit).

关于引号:如果您要搜索的值符合CSS标识符的定义(例如,您的地图值),则它们是可选的。如果您匹配的东西不符合该定义(例如任何带有空格的东西,或者以未转义的数字开头),则需要它们。

Since quotes are never wrong, best to always use them, but you don't have to if you know you're looking for a simple thing like map.

因为引号永远不会出错,所以最好总是使用它们,但如果你知道你正在寻找一个像地图这样简单的东西,你就不必这么做了。

#3


0  

Try using ~= instead. It checks for if it contains the text in cases of multiple classes.

尝试使用〜=代替。它会在多个类的情况下检查它是否包含文本。

$(":input[class~=map]").each(function(index, element) {
        map_array.push(this.className);
    });

Edit: Sorry, I should clarify. This is for matching when there are multiple classes. (If you had class="map blahmap") it would match map only, where-as *= would match both - This is more useful when you're trying to check if something has a specific class name amongst multiple classes)

编辑:对不起,我应该澄清一下。这是为了在有多个类时进行匹配。 (如果你有class =“map blahmap”)它只会匹配map,where-as * =会匹配两者 - 当你试图检查某些东西是否有多个类中的特定类名时,这会更有用)