shr*_*iek 0 javascript jquery dom knockout.js
我是Knockout的新手,我正在尝试使用一个jquery插件,它将自定义样式应用于某些元素.但是因为我有一个页面从ajax调用中获取内容并且所有元素都是通过敲除构建的,所以初始jquery函数调用不知道页面上有任何元素,因此不对这些元素应用样式.
所以我要问的是,在完成敲除操作元素(DOM)后,如何回调jquery函数?
现在我正在调用jquery函数如下: -
$(document).on("load",function(){
$(".element").callPlugin("add-style");
});
Run Code Online (Sandbox Code Playgroud)
applyBindings是同步的,所以你可以调用callPlugin后ko.applyBindings(VM)(下一行).
ko.applyBindings(VM);
$(".element").callPlugin("add-style");
Run Code Online (Sandbox Code Playgroud)
或者,如果您多次更新UI ,则可以使用自定义绑定.假设.element是<div>(它也可能是其他任何东西),你的标签看起来像这样:
<div class="element" data-bind="text: 'This is just some text which KO will bind',
updateUI: true">
This text will change. Wait for it..
</div>
Run Code Online (Sandbox Code Playgroud)
注意updateUI在data-bind.这是相应的JS代码:
ko.bindingHandlers.updateUI = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext){
// This will be called when the binding is first applied to an element
// Set up any initial state, event handlers, etc. here
$(".element").callPlugin("add-style");
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
// This will be called once when the binding is first applied to an element,
// and again whenever the associated observable changes value.
// Update the DOM element based on the supplied values here.
$(".element").callPlugin("update-style"); // just saying
}
};
Run Code Online (Sandbox Code Playgroud)
这将使您的插件在对DOM进行任何更改时自动初始化和更新.
希望这可以帮助!