看看这个小提琴:http://jsfiddle.net/BxvVp/11/
我创建了一个视图模型,该模型具有用页面上的一些隐藏内容替换div内容的功能。完成此操作后,text绑定似乎已被处理,但click绑定未被处理。
我做错了什么吗?
html:
<h4>Clicking the anchor created by clicking 'Summarize' should cause an alert, but doesn't.</h4>
<a href="#" data-bind="click: summarize">Summarize</a>
<div id="plot1"></div>
<div id="summary1" style="display:none;"> <a data-bind="text: 'anchor-text-replaced', click: function(data, event) { alert('anchor clicked!'); }" href="#">anchor-text</a>
</div>
<hr />
<h4>Clicking this anchor causes the alert as exptected.</h4>
<div id="plot2"></div>
<div id="summary2">
<a data-bind="text: 'anchor-text-replaced', click: function(data, event) { alert('anchor clicked!'); }" href="#">anchor-text</a>
</div>javascript:
var ViewModel = function () {
var self = this;
self.summarize = function () {
$("#plot1").html($("#summary1").html());
};
};
ko.applyBindings(new ViewModel());发布于 2013-04-05 23:27:28
Knockout只在你最初调用ko.applyBindings()时应用绑定,任何时候Knockout替换包含绑定的HTML,foreach块,模板等,仅仅因为你插入了一些包含绑定的随机HTML,并不意味着Knockout突然意识到这一点。
https://stackoverflow.com/questions/15837633
复制相似问题