<html>
  <script language="javascript">
    function showAllServices(){
      var allservices = document.getElementById("allservices");
      alert(allservices); 
    }  
 
  </script>
  <body>
    <select name="allservices" id="allservices" size="8" multiple ></select>
<script language="javascript">
   showAllServices();
   </script>
  </body>
</html>
加载

解决方案 »

  1.   

    <html>
      
      <body>
        <select name="allservices" id="allservices" size="8" multiple ></select>
      </body>
    <script language="javascript">
        function showAllServices(){
          var allservices = document.getElementById("allservices").value;
          alert(allservices); //为什么是null
        }  
        showAllServices();
      </script>
    </html>
      

  2.   

    页面还没载入完之前,用document.getElementById("allservices");时,该dom对象还未解析出来,当然是null 了.
    你可以试一下下面的方法:<html>
      <script language="javascript">
        function showAllServices(){
          var allservices = document.getElementById("allservices");
          alert(allservices); //为什么是null
        }  
          </script>
      <body>
        <select name="allservices" id="allservices" size="8" multiple ></select>
        <script language="javascript">
           showAllServices();
        </script>
      </body>
    </html>或<html>
      <script language="javascript">
        function showAllServices(){
          var allservices = document.getElementById("allservices");
          alert(allservices); //为什么是null
        }  
          </script>
      <body onload="showAllServices();">
        <select name="allservices" id="allservices" size="8" multiple ></select>
      </body>
    </html>