在数据绑定内发送事件目标单击 html
Send event target inside data-bind click html
在数据绑定中发送参数时无法发送事件目标。
<button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINSIPAL</button>
self.notify = function (str, id, e) {
if (!e) e = window.event;
console.log(str + id);
console.log(e.currentTarget);
});
currectTarget
的结果是undefined
。但是当不使用参数时,这将起作用。工作示例:
<button class="tablinks" data-bind="click:$root.notify;" id="defaultOpen">PRINSIPAL</button>
self.notify = function (data, e) {
console.log(e.currentTarget);
});
输出将是<button class="tablinks" data-bind="click:$root.notify;" id="defaultOpen">PRINSIPAL</button>
解析多个参数时如何获取事件目标
在 Knockoutjs
中,event
总是作为最后一个参数传递给事件处理程序。所以你可以在那里抓住它并像
一样使用它
function vm(){
this.notify = function (str, id, e,event) {
console.log(event.currentTarget)
}
}
ko.applyBindings(new vm())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>
<button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINSIPAL</button>
在数据绑定中发送参数时无法发送事件目标。
<button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINSIPAL</button>
self.notify = function (str, id, e) {
if (!e) e = window.event;
console.log(str + id);
console.log(e.currentTarget);
});
currectTarget
的结果是undefined
。但是当不使用参数时,这将起作用。工作示例:
<button class="tablinks" data-bind="click:$root.notify;" id="defaultOpen">PRINSIPAL</button>
self.notify = function (data, e) {
console.log(e.currentTarget);
});
输出将是<button class="tablinks" data-bind="click:$root.notify;" id="defaultOpen">PRINSIPAL</button>
解析多个参数时如何获取事件目标
在 Knockoutjs
中,event
总是作为最后一个参数传递给事件处理程序。所以你可以在那里抓住它并像
function vm(){
this.notify = function (str, id, e,event) {
console.log(event.currentTarget)
}
}
ko.applyBindings(new vm())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>
<button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINSIPAL</button>