當使用on進行事件綁定時當要給document綁定click,而子元素要禁止冒泡,那么子元素里面的子元素的click事件就會無效了,
下面無效版:
$('#queue').on('click', '.link', function() { var t = $(this) ,box = t.next() if(t.hasClass('active')) { box.hide() t.removeClass('active') } else { box.show() t.addClass('active') } event.stopPropagation() }) //排隊列表收縮 $(document).on('click','body',function(){ $('.link').removeClass('active') $('.queue-box').hide(); }) $('#queue').on('click','.queue-box',function(){//綁 $('#queue')或 $(document)都一樣 event.stopPropagation() //主要是下面 $(document).on('click','.btn-queue-join',function(){ mywebsocket.send(JSON.stringify({ "action": "patientJoinQueue", "patientCode": patientCode, "orderCode": $(this).parents('.item').attr('data-id') })) });
//修改$(document)為$('.queue-box')就可以了 $('.queue-box').on('click','.btn-queue-join',function(){ mywebsocket.send(JSON.stringify({ "action": "patientJoinQueue", "patientCode": patientCode, "orderCode": $(this).parents('.item').attr('data-id') })) });
參考http://www.cnblogs.com/tengj/p/4794947.html對其進行了理解
暫時沒空后面補理解