附上代码here
<button id='add'>add</button>
<div data-bind="template: 'tabsTmpl'"></div>
<script id="tabsTmpl" type="text/html">
<div data-bind="jqTabs: { fx: { } }">
<ul>
{{each tabs}}
<li>
<a href="#tab-${id}">${id}</a>
</li>
{{/each}}
</ul>
{{each tabs}}
<div id="tab-${id}" >${id}</div>
{{/each}}
</div>
</script>
<script>
ko.bindingHandlers.jqTabs = {
init: function (element, valueAccessor) {
var options = valueAccessor() || {};
$(element).tabs(options);
}
};
function TabItem(id) {
this.id = ko.observable(id);
}
var viewModel = {
tabs: ko.observableArray([new TabItem(1), new TabItem(2), new TabItem(3)])
};
$(function () {
ko.applyBindings(viewModel);
$('#add').click(function () {
viewModel.tabs.push(new TabItem(viewModel.tabs().length + 1));
});
});
</script>
以上代码作者使用jquery模板引擎,但我想使用knockout.js的原生模板引擎,所以我将html代码更改如下:
<div>
<div data-bind="jqTabs: { fx: { } }">
<ul data-bind="foreach: tabs">
<li>
<a data-bind="attr:{href:'#tab-' + id()}, text:id"></a>
</li>
</ul>
<!-- ko foreach: tabs -->
<div data-bind="attr:{id:'tab-'+id()}, text:id"></div>
<!-- /ko -->
</div>
</div>
但是,它不起作用。我对原生模板引擎做错了什么?提前致谢。