on事件綁定阻止冒泡的問題


當使用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對其進行了理解

暫時沒空后面補理解


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM