2013-07-21 86 views
2

我是JQuery中的新成员。帮助我,我得到了错误Microsoft JScript runtime Error:'$'is UndefinedJQuery:计算百分比滚动错误

See more

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
<script type="text/javascript" src="Scripts/jquery.min.js"></script> 
<script type="text/javascript">   
    $(window).scroll(function() { 
     var startValue = 70; // scrollTop value when to start incrementing 
     var stopValue = 300; // scrollTop value when to stop incrementing 
     var scrollTop = $(window).scrollTop(); 
     if (scrollTop > startValue && scrollTop <= stopValue) 
      $("#pct").text((((scrollTop - startValue)/(stopValue - startValue)) * 100).toFixed(0)); 
     else if (scrollTop <= startValue) 
      $("#pct").text(0); 
     else if (scrollTop >= stopValue) 
      $("#pct").text(100); 
    }); 
</script> 
<style type="text/css"> 
    body { 
     height:2000px; 
     } 

    #pct { 
     height:150px; 
     width:150px; 
     background-color:#369; 
     font:bold 60px verdana; 
     padding:20px; 
     position:absolute; 
     top:400px; 
     left:200px; 
    } 
    </style> 
    </head> 
<body> 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> 
hi 
<div id="pct">0</div> 
</body> 
</html> 

See jsFiddle 它的工作原理的jsfiddle但不是在IE是财产以后丢失帮助我吗? 感谢

+0

这意味着你没有正确加载jQuery。 – Barmar

+0

是的,但如何正确加载它? – YKJ

回答

0

使用最新的jQuery文件

http://jsfiddle.net/nbSNA/12/

$(window).scroll(function() { 
    var scrollTop = $(window).scrollTop(); 
    if (scrollTop > 70 && scrollTop <= 300) 
     $("#pct").text((((scrollTop-70)/(300-70))*100).toFixed(0)); 
    else if (scrollTop <= 70) 
     $("#pct").text(0); 
    else if (scrollTop >= 300) 
     $("#pct").text(100); 
}); 
+0

这不起作用。传递给事件处理程序的参数是事件,而不是jQuery函数。 – Barmar

+0

我使用这个,但我认为缺少一些东西。有没有需要初始化的东西? – YKJ

+0

是不是在工作? – Hushme

2

你最有可能缺少行:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
+0

我下载它,然后在本地使用它,但仍然得到相同的错误 – YKJ

+0

它试图加载文件时报告任何错误? – Barmar

+0

@YKJ在上面的脚本之前,您是否已经通过上面的行包含了文档的''中的脚本?仅仅将它放在文件夹中是不够的。 – Daedalus

0

确保您包括jQuery库您对来电前它,即使调用是在其他脚本中,引用它们应该出现在jquery包含之后。