[英]How can a Knockout binding be applied to the <html> element?
I would like to dynamically set the font-size on the root element using a custom Knockout bindinghandler which does a calculation based on the width of the browser window. 我想使用自定义的Knockout绑定处理程序在根元素上动态设置字体大小,该处理程序根据浏览器窗口的宽度进行计算。
When I tried to apply the binding, nothing seemed to happen, so I tried to apply a simple css binding: 当我尝试应用绑定时,似乎什么都没有发生,因此我尝试应用一个简单的CSS绑定:
<html data-bind="css: { bindinghandlertest: true }">
But the binding handler didn't seem to add the class. 但是绑定处理程序似乎没有添加类。
Question: Can KO bindings only be applied to <body>
and its children? 问题: KO绑定只能应用于
<body>
及其子代吗?
Note: I am initializing all bindings on the whole page by simply calling ko.applyBindings();
注意:我只需调用
ko.applyBindings();
就可以初始化整个页面上的所有绑定ko.applyBindings();
once on DOM ready, with no parameters at all. 一旦在DOM上就绪,就没有任何参数。
You can apply bindings to a specific html element as explained here 您可以将绑定特定HTML元素的解释这里
Specifically: 特别:
Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes.
(可选)您可以传递第二个参数来定义要搜索数据绑定属性的文档的哪一部分。 For example,
ko.applyBindings(myViewModel, document.getElementById('someElementId'))
例如
ko.applyBindings(myViewModel, document.getElementById('someElementId'))
In you case, you can them call 在这种情况下,您可以让他们致电
ko.applyBindings(myVM, document.documentElement);
by default, the DOM node is the body, as you can see from the source : 从源代码可以看到,默认情况下,DOM节点是主体:
rootNode = rootNode ||
rootNode = rootNode || window.document.body;
window.document.body; // Make "rootNode" parameter optional
//将“ rootNode”参数设为可选
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.