[英]How to alert user if shopping cart is empty? Javascript HTML
我正在建立一個網站,每當購物車中沒有東西時,都應該提醒用戶購物車中沒有東西,除非我的編碼似乎什么都沒有響應。 我如何解決它,以便代碼可以正常工作?
if (document.getElementsByClassName('cart-items').hasChildNodes()){
alert('Thanks!')
window.open("payment.html","_self");
var cartItems = document.getElementsByClassName('cart-items')[0]
while (cartItems.hasChildNodes()) {
cartItems.removeChild(cartItems.firstChild)
}
}
else {
alert("There is nothing in your cart!");
}
我希望它提醒“您的購物車中沒有東西!” 但是如果您的購物車被點擊后沒有任何提示,它不會發出任何警告。
hasChildNodes
是一個函數,但已應用於數組。 設置cartItems
變量時,您將需要[0]
。
if (document.getElementsByClassName('cart-items')[0].hasChildNodes()){ alert('Thanks!') // ... other code here } else { alert("There is nothing in your cart!"); }
<div class="cart-items"> <div>Shirt</div> <div>Trousers</div> <div>Socks</div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.