2017-04-21 96 views
0

我有以下的html代碼: products獲得從父節點父在JS

當我點擊刪除按鈕從該產品的< LI>屬性必須刪除。

它曾與JS工作,但現在它沒有這樣做。 監聽器:

cartList.on('click', '.delete-item', function(event){ 
    event.preventDefault(); 
    removeProduct($(event.target).parents('.product')); 
}); 

這是removeproduct功能,直到它崩潰了:

function removeProduct(product) { 
    clearInterval(undoTimeoutId); 
    cartList.find('.deleted').remove(); 
    var topPosition = product.offset().top - cartBody.children('ul').offset().top, 
    productQuantity = Number(product.find('.quantity').find('input').val()), 
    productTotPrice = Number(product.find('.price').text().replace('€', '')); 
} 

我得到的錯誤:

main.js:168 Uncaught TypeError: Cannot read property 'top' of undefined

所以他很可能無法獲得該節點的產品但只讀取「動作」 - 作爲父節點的第一個節點。但是,我怎麼能存取權限的產品裏屬性刪除整個行

+0

'cartBody.children( 'UL')偏移()''是undefined'。什麼是'cartBody'?它是否有任何兒童'​​ul'元素? –

+2

如果您有問題需要顯示HTML,請將其包含爲文本,而不是圖片,以便相關人員可以輕鬆地將其複製到答案中。 – nnnnnn

+0

html是用javascript編碼的 – belgiums

回答

0

嘗試使用下面的代碼片段:

cartList.on('click', '.delete-item', function(event){ 
$(this).parents('.product').remove(); //this will remove the parent .product from the DOM 
}); 
+0

。你知道我是如何得到它的「productQuantity」,現在不工作.. – belgiums

+0

productQuantity = Number(product.find('。quantity')。find('input')。val()),在div「數量」內沒有輸入標籤。 –