我使用以下JavaScript检查组合条目:
var proteins = [
"charred sirloin steak",
"roasted salmon",
"grilled chicken",
"grilled salmon",
"jerk chicken",
"miami ribs",
"charred steak",
"tandoori chicken supreme"
];
var sides = [
"israeli couscous salad",
"kale caesar",
"field o greens",
"grilled broccoli",
"greek salad",
"coconut curry quinoa salad",
"vegetable egg fried rice",
"general tao brussel sprouts",
"tokyo slaw",
"mac cheese",
"potato salad"
];
var comboEntries = document.querySelectorAll('.combo-display .combo-item');
for (var i = 0; i < comboEntries.length; i++) {
if (proteins.indexOf(comboEntries[i].innerText) == -1 || sides.indexOf(comboEntries[i].innerText) == -1) {
comboEntries[i].style.color = "orangered";
console.log(comboEntries[i].innerText + " is not a regular combo item.");
}
}
但是,这会将所有comboEntries
输出为不规则项。如果我将条件反转为!= -1
,则可以看到它正常工作,突出显示常规项目,而排除不规则项目。
我在这里做错了什么?一定是我没意识到的简单事情。
编辑:对不起,我之前没有包含HTML,因为这是生成内容的在线厨房显示屏的一部分。这是一个示例:
<div class="combo-display">
<p><strong>Protein 1:</strong> <span id="protein-1" class="combo-item">Custom item</span></p><div class="item-count"> x <span>1</span></div>
<p><strong>Protein 2:</strong> <span id="protein-2" class="combo-item">Custom item</span></p><div class="item-count"> x <span>1</span></div>
<p><strong>Protein 3:</strong> <span id="protein-3" class="combo-item">grilled chicken</span></p><div class="item-count"> x <span class="grilled-chicken-counter">1</span></div>
<p><strong>Side 1:</strong> <span id="side-1" class="combo-item">Custom item</span></p><div class="item-count"> x <span>1</span></div>
<p><strong>Side 2:</strong> <span id="side-2" class="combo-item">kale caesar</span></p><div class="item-count"> x <span class="kale-caesar-counter">1</span></div>
<p><strong>Side 3:</strong> <span id="side-3" class="combo-item">field o greens</span></p><div class="item-count"> x <span class="field-o-greens-counter">1</span></div>
</div>
因此,custom item
应该突出显示为不规则项
答案 0 :(得分:0)
尽管由于缺少HTML代码而导致您的问题尚不清楚,但是如果我没记错的话,您将尝试突出显示 值,而这两项都不在{{1} }或proteins
中都没有。
为此,您应该在sides
中使用&&
。
这是一个有效的代码段:
if condition
var proteins = [
"charred sirloin steak",
"roasted salmon",
"grilled chicken",
"grilled salmon",
"jerk chicken",
"miami ribs",
"charred steak",
"tandoori chicken supreme"
];
var sides = [
"israeli couscous salad",
"kale caesar",
"field o greens",
"grilled broccoli",
"greek salad",
"coconut curry quinoa salad",
"vegetable egg fried rice",
"general tao brussel sprouts",
"tokyo slaw",
"mac cheese",
"potato salad"
];
var comboEntries = document.querySelectorAll('.combo-display .combo-item');
for (var i = 0; i < comboEntries.length; i++) {
if (proteins.indexOf(comboEntries[i].innerText) == -1 && sides.indexOf(comboEntries[i].innerText) == -1) {
comboEntries[i].style.color = "orangered";
console.log(comboEntries[i].innerText + " is not a regular combo item.");
}
}