This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
                            
                                (9个答案)
                            
                    
                3年前关闭。
        

    

我不明白为什么简单的代码无法正常工作是我的浏览器。

    <head>
     <script src="../static/js/jquery-1.12.3.min.js" ></script>
    <script src="../static/js/jquery-ui.min.js"></script>
    <link href="../static/js/jquery-ui.min.css" rel="stylesheet"/>
    <script>
     $("#totalCart").html("99,75");
    </script>
   </head>

<body>
 <div id="totalCart">Total </div>
</body>


当我在Chrome中启动此代码时,什么都看不到,为什么?

最佳答案

在加载DOM之前,您正在使用选择。

良好的做法是将所有脚本都放在身体的末端。

或者你可以使用

$(document).ready(function(){
  //your code
});

10-05 20:49
查看更多