我无法让它工作:http: //jsfiddle.net/2jg2F/1/
我基本上想创建一个链接,当有人单击文本时更改为输入框。我试过淘汰赛 2.0 和 2.1。
HTML
<div data-bind="clickToEdit : message"> </div>
Javascript
ko.bindingHandlers.clickToEdit = {
init: function(element, valueAccessor) {
var observable = valueAccessor(),
link = document.createElement("a"),
input = document.createElement("input");
element.appendChild(link);
element.appendChild(input);
observable.editing = ko.observable(false);
ko.applyBindingsToNode(link, {
text: observable,
visible: !observable.editing,
click: function() {
observable.editing(true);
}
});
ko.applyBindingsToNode(input, {
value: observable,
visible: observable.editing,
hasfocus: observable.editing
});
}
};
function ViewModel() {
this.message = ko.observable("Click on me to edit");
}
ko.applyBindings(new ViewModel());