我目前正在開發使用ASP.NET,我想開始使用Knockout JS ......基本上我所做的是我複製粘貼了第一個教程。Knockout開始使用JS
所以我要把它放到我的head
:
<script type="text/javascript">
function() {
// This is a simple *viewmodel* - JavaScript that defines the data and
behavior of your UI
function AppViewModel() {
this.firstName = ko.observable("Bert");
this.lastName = ko.observable("Bertington");
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
this.capitalizeLastName = function() {
var currentVal = this.lastName(); // Read the current value
this.lastName(currentVal.toUpperCase()); // Write back a modified value
};
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
}();
</script>
...隨着
<script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="Scripts/knockout-2.0.0.js"></script>
在我body
我放在
<!-- This is a *view* - HTML markup that defines the appearance of your UI -->
<p>
First name: <strong data-bind="text: firstName"></strong>
</p>
<p>
Last name: <strong data-bind="text: lastName"></strong>
</p>
<p>
First name:
<input data-bind="value: firstName" /></p>
<p>
Last name:
<input data-bind="value: lastName" /></p>
<p>
Full name: <strong data-bind="text: fullName"></strong>
</p>
<button data-bind="click: capitalizeLastName">
Go caps</button>
的代碼是從全部採取淘汰賽JS的教程,但不知何故值不會自動綁定自己 - 在其他窩裏它不適合我。我在這裏錯過了什麼嗎?
你好。當我將腳本放在身體的底部時,它會起作用。雖然JavaScript總是應該被放置在頂部嗎? – matt 2012-02-03 06:41:54
最好在底部。您希望在瀏覽器開始加載腳本之前顯示這些元素,通常是*。 – eppdog 2012-02-03 06:50:08