亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復 onclick 函數無法使用 JavaScript

如何修復 onclick 函數無法使用 JavaScript

jeck貓 2023-10-10 16:32:12
有人可以幫助我嗎?這是我的問題。當我嘗試單擊button使用此功能時$(".btn_cellophane").on("click",function(){});,我沒有得到任何響應,甚至沒有得到錯誤。我猜這個onclick功能不起作用。下面是我的代碼。方法調用$(".btn_cellophane").on("click",function(){     alert("Tester true");         if($(this).hasClass("btn-warning")){              $(this).removeClass("btn-warning");              $(this).addClass("btn-success");              // add_amount_for_cellophane();         }else if($(this).hasClass("btn-success")){              $(this).addClass("btn-warning");              $(this).removeClass("btn-success");              // add_amount_for_cellophane();         }});內容展示var html_cellophane = "";$.each(data, function(key,product){        $.each(product.sizes, function(pro_id,variant){             if(variant.prod_type == 1){                   var label = (variant.size+" "+variant.unit).toUpperCase();                   var var_id = variant.variant_id;                   var price = variant.price;                       html_cellophane += '<div>'+                                                '<label>Cellophane ('+price+' pesos)</label>'+                                           '</div>'+                                           '<br/>'+              }                                              });                                                 });                  
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

您已經動態創建了該按鈕。因此,當您附加事件偵聽器時,該元素在此之前并不存在。因此,您必須使用事件委托來處理單擊事件。

只需像這樣更改您的事件偵聽器 -

$(document).on("click",".btn_cellophane",?function(){});


查看完整回答
反對 回復 2023-10-10
  • 1 回答
  • 0 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號