Knockout.js通过click事件获取元素ID [英] knockoutjs get element id through click event
本文介绍了Knockout.js通过click事件获取元素ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用基因敲除js,目前在我的视图中看起来像这样:
I'm using knockoutjs and I currently have something in my view that looks like this:
<img id="myTab1" data-bind="click: pressedTab.bind($data, '#myTab1')" src="images/image1.png"></img>
这使我可以在视图模型中获取元素ID:
This allows me to get the element ID in my view model:
pressedTab = function(tab){
console.log("Element ID: " + tab);
}
这样写:
元素ID:#myTab1
Element ID: #myTab1
但是,在click事件中发送img id的名称太重复了.有没有一种方法可以发送img id而不显式地重写它?
However, it's too repetitive to send the name of the img id in the click event. Is there a way to send the img id without explicitly re-writing it?
推荐答案
您实际上可以通过KO单击处理程序访问事件对象.
You actually can get access to the event object via a KO click handler.
<button id="somebutton" data-bind="click: log">Click Me </button>
var ViewModel = function() {
this.log = function(data, event) {
console.log("you clicked " + event.target.id);
}
};
ko.applyBindings(new ViewModel());
http://jsfiddle.net/madcapnmckay/e8JPT/
希望这会有所帮助.
这篇关于Knockout.js通过click事件获取元素ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文