Javascript 获取动态插入的 HTML 以使用 Knockoutjs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10826482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:05:18  来源:igfitidea点击:

Get dynamically inserted HTML to work with knockoutjs

javascriptjqueryhtmlknockout.jsknockout-2.0

提问by T.Ho

I'm using JQuery DataTables for all my tables because of all the nice built-in features, but it seems the only way to customize the table layout is to set the "sDom" option attribute for the DataTable and use something like $("div.SOMECLASS").html(HTML_HERE)to insert the customized html into the table. (FYI, i'm just trying to customize the header).

由于所有漂亮的内置功能,我对所有表格都使用 JQuery DataTables,但似乎自定义表格布局的唯一方法是为 DataTable 设置“sDom”选项属性并使用类似的东西$("div.SOMECLASS").html(HTML_HERE)插入自定义 html 到表格中。(仅供参考,我只是想自定义标题)。

The problem is I want the inserted html to use knockoutjs binding. Knockout doesn't seem to initialize the binding this way.

问题是我希望插入的html 使用knockoutjs 绑定。淘汰赛似乎没有以这种方式初始化绑定。

Is there a way to work around this?

有没有办法解决这个问题?

This is part of the html that I want to insert. It's pretty much a drop down list of some custom filter functions for the table.

这是我要插入的 html 的一部分。它几乎是表格的一些自定义过滤器功能的下拉列表。

'<li><a data-bind="click: Filter(\'Severity 1\', 2)">Severity 1</a></li>'

回答by Trinh Hoang Nhu

You have to call this function after insert dynamic HTML element

插入动态 HTML 元素后必须调用此函数

ko.applyBindings(viewModel, elementContainingDynamicContent)

ko.applyBindings(viewModel, elementContainingDynamicContent)

Example here http://jsfiddle.net/rniemeyer/FCN5p/

这里的例子http://jsfiddle.net/rniemeyer/FCN5p/