我用javascript做错了什么?

时间:2017-09-17 13:22:58

标签: javascript html

任何人都可以帮助我并告诉我代码有什么问题。 请帮我这个代码。我不知道那有什么问题。 任何人都可以帮助我,并告诉我代码有什么问题。 请帮我这个代码。我不知道那有什么问题。

<!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>

1 个答案:

答案 0 :(得分: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>