1

谁能告诉我为什么尝试运行此代码时我的页面上没有输出任何内容?

我正在尝试解析 localStorage 项目并使用 for 循环为每个项目创建一个表。然后我添加总价。

注意:localStorage 包含 3 个项目并且为空。

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <style>img{ height: 100px; float: left; }</style>
    <link rel="stylesheet" type="text/css" href="style.css">

    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>


    <script>

        function ShoppingCart() {

            var totalPrice = 0;
            var output;

        for (var i = 0; i < localStorage.length; i++){

           var product =  localStorage.getItem('Product_'+i);

            var result = JSON.parse(product);

            var productName;
            var productAlbum;
            var productQuantity;
            var productPrice;
            var productSubTotal = 0;
            var totalPrice;

           productName = result.name
           productAlbum = result.album;
           productQuantity = result.quantity;
           productPrice = parseFloat(result.price);
           productSubTotal = productQuantity * productPrice;

           totalPrice = totalPrice + productSubTotal;


            outputName = "<div id='cart-table'><table><tr><td>NAME:" + productName + "</td></tr></div>" ;
            outputAlbum = "<tr><td>ALBUM:" + productAlbum + "</td></tr>" ;
            outputQuantity = "<tr><td>QUANTITY:" + productQuantity + "</td></tr>";
            outputPrice = "<tr><td>PRICE:" + productPrice + "</td></tr>";
            outputSubTotal = "<tr><td>SUB-TOTAL" + productSubTotal + "</td></tr></table><br><br>";




                        }

            var outputTotal = "<table><tr><td>" + totalPrice + "</td></tr></table>";
            var TotalOutput = outputName + outputAlbum + outputQuantity + outputPrice + outputSubTotal + outputTotal;
            document.getElementById("Cart-Contents").innerHTML=TotalOutput

                    alert(TotalOutput);
        }

          window.onload = ShoppingCart();


    </script>


</head>
<body>
<div id="wrapper">

    <header id="header">
        <strong><table id="menu"><tr><td>Home</td><td>Contact</td><td>Login</td><td></td><td>Products</td></tr></table></strong>
    </header>
    <section id="middle">

        <div id="container">
            <div id="content">
                <a class=""  onclick="ShoppingCart()"><span>Cart</span></a>
                <div id="Cart-Contents">


            </div>


            </div><!-- #content-->
        </div><!-- #container-->

        <aside id="sideLeft">
            <strong>Left Sidebar:</strong>
        </aside><!-- #sideLeft -->

        <aside id="sideRight">
            <strong>Right Sidebar:</strong>
        </aside><!-- #sideRight -->

    </section><!-- #middle-->

</div><!-- #wrapper -->



<footer id="footer">
    <strong>by Brian Livori</strong>
</footer><!-- #footer -->
</body>
</html>
4

1 回答 1

2

很简单,只要改变

window.onload = ShoppingCart();

window.onload = ShoppingCart;

window.onload是一个回调函数引用。当您分配时ShoppingCart()- 您实际上将函数执行的结果分配给window.onload

这里的例子http://jsbin.com/urikub/1/edit

于 2013-04-25T00:58:22.513 回答