我在 Knockout.js 文档中找到了有关如何在切换可见绑定(bind)甚至使用“foreach”绑定(bind)的 beforeRemove 事件时添加 jquery 效果的示例。
但是,我还没有找到如何在切换“if”绑定(bind)时添加 jquery 效果
考虑以下代码:
<table data-bind="with: myModel">
<tr data-bind="if: IsVisible">
<td>some string</td>
</tr>
</table>
当 IsVisible 返回 true 时,如何添加 jquery fadeIn 效果?
最佳答案
您不能直接使用 if
绑定(bind),但在 if
绑定(bind)中使用自定义绑定(bind)可以解决问题:
<table data-bind="with: myModel">
<tr data-bind="if: IsVisible">
<!--ko fadeIn: true-->
<td>some string</td>
<!--/ko-->
</tr>
</table>
处理程序:
ko.bindingHandlers.fadeIn = {
init: function(element) {
$(ko.virtualElements.childNodes(element))
.filter(function () { return this.nodeType == 1; })
.hide()
.fadeIn();
}
};
ko.virtualElements.allowedBindings.fadeIn = true;
关于jquery - 为 knockout "if"绑定(bind)添加效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15510178/