Prevent event bubbling when using the checked binding in knockoutjs

后端 未结 1 1795
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 09:02

I\'m building a UI using KnockoutJs and Twitter Bootstrap.

I\'m using the checked binding inside a Bootstrap dialogue called dropdown-toggle

1条回答
  •  Happy的楠姐
    2020-11-30 09:30

    It sounds like you were on the right track. You basically want to do the equivalent of:

    click: function() { return true; }, clickBubble: false
    

    OR This could be done in a custom binding like:

    ko.bindingHandlers.stopBubble = {
      init: function(element) {
        ko.utils.registerEventHandler(element, "click", function(event) {
             event.cancelBubble = true;
             if (event.stopPropagation) {
                event.stopPropagation(); 
             }
        });
      }
    };
    

    The normal click/event handler attached by KO will prevent the default action unless you return true. However, if we hook up our own event handler, then we only need to prevent it from bubbling.

    Sample: http://jsfiddle.net/MikeEast/PyNfg/1/

    0 讨论(0)
提交回复
热议问题