这是一个更新的小提琴:http: //jsfiddle.net/jearles/Jbp7y/147/
HTML
<div><button data-bind="click: clickMe">Click Me!</button></div>
JS
var ViewModel = function() {
var self = this;
self.clickMe = function(data,event) {
var target;
if (event.target) target = event.target;
else if (event.srcElement) target = event.srcElement;
if (target.nodeType == 3) // defeat Safari bug
target = target.parentNode;
target.parentNode.innerHTML = "something";
}
}
ko.applyBindings(new ViewModel());
我改变的事情:
- 将“validationElement”绑定添加到控制组 div
- 检测到错误时添加对“showAllMessages()”的调用
- 确保 errorElementClass 配置设置为“错误”