0

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

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 或其他东西?

4

1 回答 1

2

我在这里做了一个JSFiddle 这里右键是绑定的,你可以通过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);
于 2013-05-24T15:24:22.813 回答