knockoutjs通过点击事件获得(真实绑定)元素 [英] knockoutjs get the (real bound) element through click event

查看:270
本文介绍了knockoutjs通过点击事件获得(真实绑定)元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅此问题。除非在单击子元素时答案返回子元素,即在绑定div的情况下。

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>

请参阅小提琴

我想获得click事件绑定的原始元素。有什么建议吗?

I want to get the original element the click event was bound to. Any suggestions?

推荐答案

event.currentTarget 会给你元素哪个事件受到约束。更改您的Console.log如下:

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);

这篇关于knockoutjs通过点击事件获得(真实绑定)元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆