![](/img/trans.png)
[英]Uncaught TypeError: cannot set property 'backgroundColor' of undefined javascript
[英]Uncaught TypeError: Cannot set property 'backgroundColor' of undefined
function put_to_cart_event(prodtitle, price, clicked_cell){
var td = clicked_cell;
alert(td);
put_to_cart_({title: prodtitle, price: price}, function () {
td.style.backgroundColor = '#859de6'; // <--- problem
});
}
警報給出[object Object]
,這意味着clicked_cell
可用且可訪問,對吧?
我在這里想念什么?
該函數被這樣調用:
$('.abindenwarenkorb').on('click', function(){
var productname = $('#productname').text();
var price = $('#price').text();
var in_stock = $('#in_stock').text();
var clicked_cell = $('#clicked_cell').val(); //value is set like $('#clicked_cell').val($(this));
put_to_cart_event(productname, price, clicked_cell); // <-- call
});
可能是因為clicked_cell
沒有style屬性。 您可以在console.log中輸出clicked_cell變量嗎?
您需要傳遞元素而不是值更改下面的行
var clicked_cell = $('#clicked_cell');
見下面的代碼:
$('.abindenwarenkorb').on('click', function(){
var productname = $('#productname').text();
var price = $('#price').text();
var in_stock = $('#in_stock').text();
var clicked_cell = $('#clicked_cell');
put_to_cart_event(productname, price, clicked_cell); // <-- call
});
編輯:在進一步閱讀您的代碼時,我注意到此評論。
//value is set like $('#clicked_cell').val($(this));
此代碼無效$('#clicked_cell').val($(this))
您無法將對象設置為值。 僅允許可序列化的值。 否則,它將轉換為string,在您的情況下為[object Object]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.