2017-09-17 68 views
-5

任何人都可以幫助我,告訴我代碼有什麼問題。 請幫我用這段代碼。我不知道那有什麼問題。 任何人都可以幫助我,告訴我代碼有什麼問題。 請幫我用這段代碼。我不知道那有什麼問題。我在做什麼錯誤的JavaScript?

<!DOCTYPE html> 
<html> 
<body> 
<p id="demo">enter code here</p> 

<script> 


var all = [ 
    { 
     "name": "redfruit", 
     "price": 9.20, 
     "text2": "Our selection of red fruits and fresh milk<br>No added preservatives or sugars" 
    }, 
    { 
     "name": "veggie", 
     "price": 8.80, 
     "text2": "Our selection of veggies and fresh milk<br>No added preservatives or sugars" 
    }, 
    { 
     "name": "jajko", 
     "price": 6.90, 
     "text2": "Our selection of veggies and fresh milk<br>No added preservatives or sugars" 
    } 
]; 

var i; 
for(i=0;i<all.length;i++) 
{var moko=all[i]; 
var extrahtml='\ 
     <li>\ 
       <div class="info">\ 
        <h3>' + moko.name + '</h3>\ 
        <p>' + moko.price.toFixed(2) +' </p>\ 
        <p>' + moko.text2 + '</p>\ 

        '</div>\ 
       </li>\ 
     '; 



document.getElementById("demo").innerHTML += extrahtml; 
} 
</script> 

</body> 
</html> 
+0

有什麼錯誤?你能告訴我們HTML頁面是什麼樣子嗎?你想得到什麼? – Edric

回答

0

請參見下面的更新,你錯過了/弄丟了單引號,我希望這是你在找什麼:

<!DOCTYPE html> 
<html> 
<body> 
<p id="demo">enter code here</p> 

<script> 
    var all = [ 
     { 
      "name": "redfruit", 
      "price": 9.20, 
      "text2": "Our selection of red fruits and fresh milk<br>No added preservatives or sugars" 
     }, 
     { 
      "name": "veggie", 
      "price": 8.80, 
      "text2": "Our selection of veggies and fresh milk<br>No added preservatives or sugars" 
     }, 
     { 
      "name": "jajko", 
      "price": 6.90, 
      "text2": "Our selection of veggies and fresh milk<br>No added preservatives or sugars" 
     } 
    ]; 

    var i; 
    for (i = 0; i < all.length; i++) { 
     var moko = all[i]; 
     var extrahtml = '\ 
    <li>\ 
      <div class="info">\ 
       <h3>' + moko.name + '</h3>\ 
       <p>' + moko.price.toFixed(2) + ' </p>\ 
       <p>' + moko.text2 + '</p>\ 
       </div>\ 
      </li>\ 
    '; 
     document.getElementById("demo").innerHTML += extrahtml; 
    } 
</script> 

+0

格式文本 –

+3

中只有一個報價缺失您的回答對於解釋確切的錯誤以及如何修復它會更有價值。 – Pointy

+0

添加評論。 –