I have a function where I am iterating over each .marker
, creating a variable that contains its classes.
我有一个函数,我在每个.marker上迭代,创建一个包含其类的变量。
I also have an array named checkBoxClasses
.
我还有一个名为checkBoxClasses的数组。
What I'm having a problem with is checking the classes within the variable markerClasses
against the array checkBoxClasses
. I want to break down the variable markerClasses
and pass each individual class through the array.
我遇到的问题是检查变量markerClasses中的类与数组checkBoxClasses。我想分解变量markerClasses并通过数组传递每个单独的类。
Here's the code so far:
这是迄今为止的代码:
$('.marker').each(function () {
var markerClasses = $(this).attr('class').split(' ');
if ($.inArray(markerClasses , checkBoxClasses) > -1) {
//do something
};
});
1 个解决方案
#1
4
inArray
checks for a single value in an array. Since the value of the array reference markerClasses
isn't in checkBoxClasses
, it will always return -1.
inArray检查数组中的单个值。由于数组引用markerClasses的值不在checkBoxClasses中,因此它将始终返回-1。
It's unclear what you want to do. If you want to know if any of the markerClasses
entries is in checkBoxClasses
, you'll need to loop them and check them individually, breaking on the first match. If you want to check if they're all in checkBoxClasses
, it's similar but you break on the first non-match.
目前还不清楚你想做什么。如果你想知道任何markerClasses条目是否在checkBoxClasses中,你需要循环它们并单独检查它们,打破第一个匹配。如果你想检查它们是否都在checkBoxClasses中,它是相似的,但是你在第一次不匹配时就会中断。
E.g., to see if any of the element's classes is in checkBoxClasses
:
例如,查看任何元素的类是否在checkBoxClasses中:
var markerClasses = $(this).attr('class').split(' ');
var found = false;
$.each(markerClasses, function(index, value) {
if ($.inArray(value, checkBoxClasses) !== -1) {
found = true;
return false;
}
}
if (found) {
// At least one of the element's classes was in `checkBoxClasses`
}
To see if all of the element's classes are in checkBoxClasses
:
要查看所有元素的类是否都在checkBoxClasses中:
var markerClasses = $(this).attr('class').split(' ');
var allFound = true;
$.each(markerClasses, function(index, value) {
if ($.inArray(value, checkBoxClasses) === -1) {
allFound = false;
return false;
}
}
if (allFound) {
// *All* of the element's classes was in `checkBoxClasses`
// (Including the case where the element didn't have any.)
}
#1
4
inArray
checks for a single value in an array. Since the value of the array reference markerClasses
isn't in checkBoxClasses
, it will always return -1.
inArray检查数组中的单个值。由于数组引用markerClasses的值不在checkBoxClasses中,因此它将始终返回-1。
It's unclear what you want to do. If you want to know if any of the markerClasses
entries is in checkBoxClasses
, you'll need to loop them and check them individually, breaking on the first match. If you want to check if they're all in checkBoxClasses
, it's similar but you break on the first non-match.
目前还不清楚你想做什么。如果你想知道任何markerClasses条目是否在checkBoxClasses中,你需要循环它们并单独检查它们,打破第一个匹配。如果你想检查它们是否都在checkBoxClasses中,它是相似的,但是你在第一次不匹配时就会中断。
E.g., to see if any of the element's classes is in checkBoxClasses
:
例如,查看任何元素的类是否在checkBoxClasses中:
var markerClasses = $(this).attr('class').split(' ');
var found = false;
$.each(markerClasses, function(index, value) {
if ($.inArray(value, checkBoxClasses) !== -1) {
found = true;
return false;
}
}
if (found) {
// At least one of the element's classes was in `checkBoxClasses`
}
To see if all of the element's classes are in checkBoxClasses
:
要查看所有元素的类是否都在checkBoxClasses中:
var markerClasses = $(this).attr('class').split(' ');
var allFound = true;
$.each(markerClasses, function(index, value) {
if ($.inArray(value, checkBoxClasses) === -1) {
allFound = false;
return false;
}
}
if (allFound) {
// *All* of the element's classes was in `checkBoxClasses`
// (Including the case where the element didn't have any.)
}