2014-09-25 60 views
-1

我有简单的网页有HTML,CSS和jQuery。此页面的目的是演示水平折叠窗格。为什么对象在jQuery中的预期错误?

<html> 

<head> 
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> 
    <title>Sample HTML</title> 
    <style type="text/css"> 
     html, body { 
      width: 100%; 
      height: 100%; 
     } 

     #map { 
      width: 10%; 
      height: 100%; 
      float: left; 
     } 

     #sidebar { 
      width: 89%; 
      height: 100%; 
      float: left; 
      border: 1px solid; 
     } 

     #toggle { 
      height: 10%; 
      float: right; 
     } 
    </style> 
    <script type="text/javascript"> 
     $(window).load(function() {//this is error line 

      $(document).ready(function() { 
       $("#toggle").click(function() { 
        if ($(this).data('name') == 'show') { 
         $("#sidebar").animate({ 
          width: '10%' 
         }).hide() 
         $("#map").animate({ 
          width: '89%' 
         }); 
         $(this).data('name', 'hide') 
        } else { 
         $("#sidebar").animate({ 
          width: '89%' 
         }).show() 
         $("#map").animate({ 
          width: '10%' 
         }); 
         $(this).data('name', 'show') 
        } 
       }); 
      }); 
     }); 
    </script> 


    </head> 
<body> 

    <div id="map"> 
     <input type="button" data-name="show" value="Toggle" id="toggle"></div> 
    <div id="sidebar">SIDEBAR</div> 
</body> 
</html> 

我用IE浏览器的调试器,它在$(window).load(function()击中说预期的对象。我不明白为什么它不起作用。

此页面也需要很长时间才能加载。

+0

你使用像'文件的文件协议加载HTML文件://路径到file' – 2014-09-25 05:22:34

+0

@ArunPJohny号我这是救了我的桌面上。我打开它使用IE和Chrome,都不工作。 – 2014-09-25 05:25:41

+0

*“它保存在我的桌面上”*所以你*从文件系统加载而不是从网络服务器加载?仅供参考,您只需要'(window).load(...)'或'$(document).ready(...)',但不能同时嵌套。 – 2014-09-25 05:26:21

回答

0

你不需要使用$(window).load(function() {作为$(document).ready有监守document.ready火灾时,DOM是准备就绪,window.load火灾图像或任何其他资源负载时。有关更多信息,请参阅this

你的错误是因为jQuery库路径的到来是不正确,那么再次检查以下路径

<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> 

我觉得应该有http://

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script> 

启动,您可以纠正后删除window.load lib路径,它应该工作。

<script type="text/javascript"> 

      $(document).ready(function() { 
       $("#toggle").click(function() { 
        if ($(this).data('name') == 'show') { 
         $("#sidebar").animate({ 
          width: '10%' 
         }).hide() 
         $("#map").animate({ 
          width: '89%' 
         }); 
         $(this).data('name', 'hide') 
        } else { 
         $("#sidebar").animate({ 
          width: '89%' 
         }).show() 
         $("#map").animate({ 
          width: '10%' 
         }); 
         $(this).data('name', 'show') 
        } 
       }); 
      }); 
    </script> 
+1

请解释为什么使用'$(window).load(function(){'在这种情况下会产生错误中提到的错误。 – 2014-09-25 05:27:32

+1

澄清为什么评论:虽然你的建议当然是正确的,但它并不能解决 – 2014-09-25 05:32:30

+0

我不知道提到的错误,但是当DOM准备好时'document.ready'激发,当所有的资源,DOM准备好时'window.load'都会触发,所以没有必要使用'document .ready'里面的'window.load'。作为使用jQuery脚本里面的DOM元素的OP,没有'window.load'这样的用法,我找到了错误背后的确切原因。 – 2014-09-25 05:38:49

相关问题