knockoutjs get the (real bound) element through click event

你说的曾经没有我的故事 提交于 2020-01-03 13:34:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!