4 回答

TA貢獻1752條經(jīng)驗 獲得超4個贊
如下替換您的if
條件,它將按預期工作。它會發(fā)現(xiàn)elements
里面drinks
有 class ob-menu-item__out-of-stock
。
if (drink.getElementsByClassName("ob-menu-item__out-of-stock").length > 0)
請參考這個說不.getElementsByClassName("home")[0]
應該使用的答案。您也可以.querySelectorAll()
像下面這樣使用。替換getElementsByClassName
為querySelectorAll
并傳遞class name
給class selector (.)
。所以document.getElementsByClassName("ob-menu-item__title")[i]
將被替換為document.querySelectorAll(".ob-menu-item__title")[i]
.
要查找elements
內(nèi)部selected
元素,您可以使用element.querySelectorAll
which is done inside if
withdrink.querySelectorAll(".ob-menu-item__out-of-stock").length
for (var i = 0; i < 7; i++) {
// iterating over each drink.
var drink = document.querySelectorAll(".ob-menu-item__title")[i];
if (drink.querySelectorAll(".ob-menu-item__out-of-stock").length > 0) {
// There's out of stock text
// Do nothing and go to the next drink
} else {
//The product is available. Clik the drink and exit the loop
drink.click();
break;
}
}
在下面嘗試。
// get all drink
var drinks = document.querySelectorAll(".ob-menu-item__title");
// iterating over each drink.
for (var i = 0; i < drinks.length; i++) {
let drink = drinks[i];
if (drink.querySelectorAll(".ob-menu-item__out-of-stock").length > 0) {
console.log('out of stock. i = ' + i);
// There's out of stock text
// Do nothing and go to the next drink
} else {
//The product is available. Clik the drink and exit the loop
console.log('In stock. i = ' + i);
drink.click();
break;
}
}
<div class='ob-menu-items__items'>
<h4 class='ob-menu-item__title'>
item0<span class='ob-menu-item__out-of-stock'> - Out of stock</span>
</h4>
<h4 class='ob-menu-item__title'>
item4<span class='ob-menu-item__out-of-stock'> - Out of stock</span>
</h4>
<h4 class='ob-menu-item__title'>
item2<span class='ob-menu-item__out-of-stock'> - Out of stock</span>
</h4>
<h4 class='ob-menu-item__title'>
item3
</h4>
<h4 class='ob-menu-item__title'>
item4
</h4>
<h4 class='ob-menu-item__title'>
item5
</h4>
<h4 class='ob-menu-item__title'>
item6
</h4>
</div>

TA貢獻1790條經(jīng)驗 獲得超9個贊
您可以使用它childElementCount
來查找有多少項。請參閱https://www.w3schools.com/jsref/prop_element_childelementcount.asp

TA貢獻1856條經(jīng)驗 獲得超5個贊
for 循環(huán)初始化中的硬編碼7看起來不太好。你可以找到所有的飲料,document.querySelectorAll然后通過檢查每一種飲料的跨度來找到要點擊的飲料。
ES6單行:
[...document.querySelectorAll('.ob-menu-item__title')]
.find( drink => !drink.querySelector('.ob-menu-item__out-of-stock') )
.click()
它所做的是:將querySelectorAll結果轉(zhuǎn)換為數(shù)組,然后使用Array.prototype.findtrue方法返回滿足回調(diào)的第一個元素,如果給定元素不包含“缺貨”,則回調(diào)在這種情況下返回“ 跨度。
更多“經(jīng)典”JS:
var firstInStock = Array.from(document.querySelectorAll('.ob-menu-item__title'))
.find( function(drink){
if( drink.querySelector('.ob-menu-item__out-of-stock') ){
return false;
}
return true;
});
firstInStock.click()
或者如果你真的想要一個 for 循環(huán):
var drinks = document.querySelectorAll('.ob-menu-item__title');
for(var i=0; i< drinks.length; i++){
if( !drink.querySelector('.ob-menu-item__out-of-stock') ){
drink.click();
break;
}
}

TA貢獻1878條經(jīng)驗 獲得超4個贊
for (var i = 0; i < 7; i++) {
var drink = document.getElementsByClassName("ob-menu-item__title")[i];
if (document.getElementsByClassName.("ob-menu-item__out-of-stock").parents(drink).length == 1) {
// There's out of stock text
// Do nothing and go to the next drink
} else {
//The product is available. Clik the drink and exit the loop
document.getElementsByClassName("ob-menu-item__title")[i].click();
break;
}
}
添加回答
舉報