2017-10-19 14 views
0

新來這個,我不能讓這個簡單的淘汰賽應用程序工作。它應該顯示淘汰js應用程序 - 不能正常工作

你好,行星地球!

我引用:http://knockoutjs.com/examples/helloWorld.html

下面是當我運行中的index.html:

enter image description here

難道沒有找到淘汰賽,3.4.2.js文件?

這裏是控制檯。看起來有錯誤。

enter image description here

下面是index.html文件 「:

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Hello World</title> 

    <!-- Import the Knockout file. --> 
    <script type="text/javascript" src="C:\Dans\Work 2\Tech\Web Dev\Javascript and jQuery\Knockout.js\Examples\knockout-3.4.2.js"></script> 

    <!-- Import the JavaScript file. --> 
    <script type="text/javascript" src="app.js"></script> 
</head> 

<body> 
    <div class='liveExample'> 
    <p>First name: <input data-bind='value: firstName' /></p> 
    <p>Last name: <input data-bind='value: lastName' /></p> 
    <h2>Hello, <span data-bind='text: fullName'> </span>!</h2> 
    </div> 
</body> 

這裏的視圖模型文件 - app.js文件」:

var ViewModel = function(first, last) { 
    this.firstName = ko.observable(first); 
    this.lastName = ko.observable(last); 

    this.fullName = ko.computed(function() { 
     return this.firstName() + " " + this.lastName(); 
    }, this); 
}; 

ko.applyBindings(new ViewModel("Planet", "Earth")); 

這裏就是我有它存儲:

enter image description here

+1

你可以使用C:\ ....作爲src嗎?如果您使用像https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js這樣的CDN,會發生什麼情況? – ggdx

+1

您的瀏覽器控制檯中是否有錯誤? (ctrl-shift-i) –

+0

我試過CDN ..它沒有改變行爲。 – user3020047

回答

1

由於app.js被加載在<head>部主體的html存在之前就將其裝入,這意味着沒有任何用於基因敲除結合在時間applyBindings被調用。

<head> 
    <title>Hello World</title> 

    <!-- Import the Knockout file. --> 
    <script type="text/javascript" src="C:\Dans\Work 2\Tech\Web Dev\Javascript and jQuery\Knockout.js\Examples\knockout-3.4.2.js"> 

    <!-- Import the JavaScript file. --> 
    <script type="text/javascript" src="app.js"></script> 
</head> 

您需要將導入行移動到html正文的下面,或者將其包裝在延遲執行塊(如document.onload)中。

<head> 
    <title>Hello World</title> 

    <!-- Import the Knockout file. --> 
    <script type="text/javascript" src="C:\Dans\Work 2\Tech\Web Dev\Javascript and jQuery\Knockout.js\Examples\knockout-3.4.2.js"></script> 
</head> 

<body> 
    <div class='liveExample'> 
    <p>First name: <input data-bind='value: firstName' /></p> 
    <p>Last name: <input data-bind='value: lastName' /></p> 
    <h2>Hello, <span data-bind='text: fullName'> </span>!</h2> 
    </div> 
</body> 

<script type="text/javascript"> 
    <!-- Import the JavaScript file. --> 
    <script type="text/javascript" src="app.js"></script> 
</script>