问题
See this question. Except that the answer returns the child element when a child element is clicked, i.e. in the case that you bind a div.
<div id="parent" data-bind="click: log">Parent Div<div id="child">Child</div></div>
<script>
var ViewModel = function() {
this.log = function(data, event) {
console.log("you clicked " + event.target.id);
}
};
ko.applyBindings(new ViewModel());
</script>
See this fiddle
I want to get the original element the click event was bound to. Any suggestions?
回答1:
event.currentTarget will give you the element to which the event is bound. Change your Console.log as below:
console.log("you clicked " + event.currentTarget.id);
来源:https://stackoverflow.com/questions/14101548/knockoutjs-get-the-real-bound-element-through-click-event