![](/img/trans.png)
[英]knockout data-bind="html: html , inside html click event is not working
[英]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>
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.