Knockoutjs 绑定鼠标悬停或 Jquery
我在 jquery 中有这段代码。我该如何用绑定将其写在淘汰赛中。 或者把它放在 jquery 中更好。有没有一种聪明的方法来知道何时将其写入绑定或 jquery 中?这是更多的视图,所以也许它应该在 Jquery 中?
$("body").on("mouseover mouseout", '.hoverItem', function () {
$(this).toggleClass('k-state-selected');
});
I have this code in jquery. How would I write this in knockout with binding.
Or is it better to have it in jquery. Is there a smart way to know when to write it in binding or in jquery? This is more viewstuff so maybe it should be in Jquery ?
$("body").on("mouseover mouseout", '.hoverItem', function () {
$(this).toggleClass('k-state-selected');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果此类的切换实际上并不需要连接到视图模型中的数据,那么您确实没有理由不能执行现在正在执行的操作。
以下是如何使用默认绑定执行此操作的示例:
view:
视图模型代码:
使用自定义绑定,您甚至可以将其简化为:
view model:
您甚至可以变得更复杂,并让它以委托方式工作,例如 < code>on 通过将绑定置于更高级别并基于选择器应用切换。
就经验而言,我认为当功能不需要视图模型中的数据时,开发人员是否想要使用连接代码或以声明方式执行它取决于开发人员。在您的情况下,可能足以坚持使用
on
直到它需要来自视图模型的数据。If the toggling of this class doesn't really need to connect to the data in your view model, then there is really no reason that you couldn't do what you are doing now.
Here is a sample of how to do it with the default bindings:
view:
view model code:
With custom bindings you could even reduce it down to:
view model:
You could even get more sophisticated and have it work in a delegated fashion like
on
by putting the binding at a higher level and applying the toggle based on a selector.As far as a rule of thumb, I think that it is up to the developer whether they want to use wire up code or do it declaratively when the functionality does not require data from the view model. In your case, probably good enough to stick with
on
until it requires data from your view model.