Javascript 敲除(js)到(html)头部
敲除在HTML文档的头部不起作用吗 我正在尝试绑定样式表的href-Javascript 敲除(js)到(html)头部,javascript,knockout.js,Javascript,Knockout.js,敲除在HTML文档的头部不起作用吗 我正在尝试绑定样式表的href- <html> <head> <link type="text/css" data-bind="attr: { href: myStyleSheet }" rel="stylesheet"> </head> <body> <script> var ViewModel = function() { this.myStyleSheet = ko.o
<html>
<head>
<link type="text/css" data-bind="attr: { href: myStyleSheet }" rel="stylesheet">
</head>
<body>
<script>
var ViewModel = function() {
this.myStyleSheet = ko.observable('/css/stylea.css');
};
ko.applyBindings(new ViewModel());
</script>
</body>
</html>
var ViewModel=函数(){
this.myStyleSheet=ko.observable('/css/stylea.css');
};
应用绑定(新的ViewModel());
动态dom看起来就像这样-
<link rel="stylesheet" type="text/css" data-bind="attr: { href: myStyleSheet }">
什么也没有发生。您可以将目标指定为
ko.applyBindings()
的第二个参数。我不确定您是否可以指定头
,请尝试一下。如果未指定任何内容,则会导致以下情况:
在这里,您可以看到它选择了窗口.document.body
作为默认值,这样就不会得到标题
会有帮助吗?
ko.applyBindings = function (viewModelOrBindingContext, rootNode) {
// If jQuery is loaded after Knockout, we won't initially have access to it. So save it here.
if (!jQueryInstance && window['jQuery']) {
jQueryInstance = window['jQuery'];
}
if (rootNode && (rootNode.nodeType !== 1) && (rootNode.nodeType !== 8))
throw new Error("ko.applyBindings: first parameter should be your view model; second parameter should be a DOM node");
rootNode = rootNode || window.document.body; // Make "rootNode" parameter optional
applyBindingsToNodeAndDescendantsInternal(getBindingContext(viewModelOrBindingContext), rootNode, true);
};