如何使用Knockout绑定特定的鼠标按钮?

时间:2013-05-24 15:16:56

标签: knockout.js

很抱歉新手的问题,但我在文档中看不到这个:

http://knockoutjs.com/documentation/click-binding.html

<div>
    You've clicked <span data-bind="text: numberOfClicks"></span> times
    <button data-bind="click: incrementClickCounter">Click me</button>
</div>

<script type="text/javascript">
    var viewModel = {
        numberOfClicks : ko.observable(0),
        incrementClickCounter : function() {
            var previousCount = this.numberOfClicks();
            this.numberOfClicks(previousCount + 1);
        }
    };
</script>

如果我需要不同的操作,具体取决于是否单击了鼠标左键,右键或鼠标中键?是否有特定的绑定,或者你是否挂钩事件并使用event.which或什么?

1 个答案:

答案 0 :(得分:2)

我做了JSFiddle here这里右键是绑定,你可以通过data-bind="event: { contextmenu: incrementClickCounter }"来完成 中鼠我还没弄明白。希望这个答案可以帮助您。让我知道你的想法;)

HTML:

<div>
    You've clicked <span data-bind="text: numberOfClicks"></span> times
    <button data-bind="click: incrementClickCounter">Click me</button>
    <button data-bind="event: { contextmenu: incrementClickCounter }">Click me</button>
</div>

JavaScript的:

var viewModel = {
        numberOfClicks : ko.observable(0),
        incrementClickCounter : function() {
            var previousCount = this.numberOfClicks();
            this.numberOfClicks(previousCount + 1);
        }
    };
ko.applyBindings(viewModel);