[英]knockout(js) to the (html)head
剔除在HTML文檔的開頭是否無效?
我正在嘗試綁定樣式表的href-
<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>
動態dom看起來像這樣-
<link rel="stylesheet" type="text/css" data-bind="attr: { href: myStyleSheet }">
什么都沒發生。
您可以將目標指定為ko.applyBindings()
的第二個參數。 我不確定是否可以指定head
,請嘗試一下。 如果您未指定任何內容,它將插入以下代碼 :
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);
};
在這里,您可以看到它選擇window.document.body
作為默認值,這樣就不會出現head
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.