簡體   English   中英

單擊事件以排除子項(backbone.js)

[英]Click event to exclude children (backbone.js)

我有2個元素,一個嵌套在另一個元素中。 (它是一個模式對話框)。 當用戶點擊外元件,功能closeModal應觸發,如果用戶點擊一個li的內部元件(其子)內,另一功能like應被觸發。

問題:我查看了其他解決方案,並嘗試將其應用於我的ribs.js視圖,但似乎不起作用。 是否需要更改其他內容?

視圖

ModalShowItemView = Backbone.View.extend({
    el: '#modal_show_item',

    events: {
        'click div#modal': 'closeModal',
        'click li#like' : 'like'
    },

    initialize: function() {
        this.render();
        this.clickHandler();
    },

    render: function() {
        $(this.el).show().append( this.template( this.model.toJSON() ) );
    },

    clickHandler: function() {
        var self = this;
        $(this.el).click(function(e) {
            if(e.target == self) {
                self.closeModal();
            }
        });
    },

    closeModal: function() {
        console.log('closemodal');
    },

    like: function() {
        console.log('like');
    }
});

您提到的其他解決方案可能是您需要做的,除了在event對象本身上像.stopPropagation()那樣執行.stopPropagation() event.stopPropagation() 我不知道通過delegateEvents事件綁定的事件處理程序會傳遞任何相關的事件對象,但是它沒有記錄在幫助頁面AFAIK上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM