简体   繁体   English

与绑定一起使用时,KnockoutJS click数据绑定不起作用

[英]KnockoutJS click data bind not working when used with with binding

What I'm trying to do is to hide an html when a flag, selected, is true. 我想做的是当选定的标志为true时隐藏html。 Which I was able to do via click data bind and below works great. 我能够通过点击数据绑定完成此操作,以下操作效果很好。

<a href='#' data-bind='click: selected, with: myObject, visible: !selected()'>
    test
</a>

While above works as I intended it to be, below doesn't work. 虽然上面的方法按我的预期工作,但下面的方法却无效。

<a href='#' data-bind='click: selected, with: myObject, visible: !selected()'>
    <div>test</div>
</a>

I have tried changing 'a' to 'div' but that still didn't work. 我曾尝试将'a'更改为'div',但这仍然行不通。 Can somebody help me understand what is going on? 有人可以帮助我了解发生了什么吗?

Ah. 啊。 I misunderstood what you were struggling with. 我误会了你在努力。

The problem is that with only applies to child nodes. 问题在于with仅适用于子节点。 So, you need a wrapper around the <a> tag in order to apply the with binding. 因此,您需要在<a>标记周围使用包装器才能应用with绑定。

Updated fiddle 更新的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM