KnockoutJS - HTML 绑定
HTML 绑定使关联的 DOM 元素显示由参数指定的 HTML。如果您想动态生成 HTML 标记,这非常有用。
语法
html: <binding-value>
参数
KnockoutJS 将 DOM 元素的内容设置为提供的参数值。此功能在 JQuery 中也可用。如果 JQuery 不可用,则使用 KO 来实现这一点。
如果参数是可观察的,则当底层可观察的发生变化时,元素值会更新。如果未使用 可观察对象,则元素仅被处理一次。
示例
让我们看一下以下示例,该示例演示了如何使用 html 绑定。
<!DOCTYPE html> <head> <title>KnockoutJS Html binding</title> <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type = "text/javascript"></script> </head> <body> <p><span data-bind="html: welcomeMessgae "></span></p> <script> function AppViewModel() { this.welcomeMessgae = ko.observable(); this.welcomeMessgae ("<strong>Welcome to TutorialsPoint !!! For free online tutorials and courses click <a href = 'https://tutorialspoint.com/'>here</a>.</strong>"); } ko.applyBindings(new AppViewModel()); </script> </body> </html>
输出
让我们执行以下步骤来查看上述代码的工作原理 −
将上述代码保存在 html-bind.htm 文件中。
在浏览器中打开此 HTML 文件。
knockoutjs_declarative_bindings.html