2012-01-31 32 views
33

有人能告訴我這個http://jsfiddle.net/Yp8Bz/有什麼問題嗎?它在Chrome/Firefox中正常工作,但在IE 8中出現以下錯誤:IE中敲除JS 2.0綁定解析錯誤

Message: Unable to parse bindings. 
Message: SyntaxError: Expected identifier, string or number; 
Bindings value: click: blah, attr: {class: 'Hi'} 
Line: 38 
Char: 359 
Code: 0 
URI: http://cdnjs.cloudflare.com/ajax/libs/knockout/2.0.0/knockout-min.js 

回答

66

class放在引號內。使用保留字時遇到同樣的問題。

attr: {'class': 'Hi'} 
+5

謝謝你,這救了我的天! – bouke 2012-07-05 09:26:31

+9

這也適用於'if'。如果有人發現這個網頁有我遇到的同樣的問題。 – 2012-11-02 10:43:53

+0

@RichardDalton:你是什麼意思,它適用於?例如:如果PropertyName == 1 && SubCollection.length?應該如果'PropertyName'?和SubCollection.length?詢問,因爲我在IE8上遇到了淘汰賽2.3.0的一些問題。預期的標識符,字符串或數字行2000我把所有的屬性放在引號中if也是如此。同樣的廢話(我真的很討厭IE):( – firepol 2013-08-30 15:25:49

4

丹尼爾A.懷特已經指出了您的具體問題,但如果你要與CSS類定期的工作,我會建議使用css結合。

http://knockoutjs.com/documentation/css-binding.html

<div data-bind="css: { profitWarning: currentProfit() < 0 }"> 
    Profit Information 
</div> 

<script type="text/javascript"> 
    var viewModel = { 
     currentProfit: ko.observable(150000) // Positive value, so initially we don't apply the "profitWarning" class 
    }; 
    viewModel.currentProfit(-50); // Causes the "profitWarning" class to be applied 
</script>