jQuery / Foundation:未捕获TypeError:对象#<object>没有方法'hover'</object>

时间:2013-05-09 14:45:27

标签: javascript jquery zurb-foundation

以下代码(Foundation 4,jQuery)

<script>
$(document).ready(function(){

    $("#important img").hover(function() {
      alert("");
    });

});
</script>
</body>

给出以下错误:

  

未捕获的TypeError:对象#没有方法'悬停'

通常这个错误是由缺少$引起的,但在这种情况下不是?

编辑:以下是基础4 html在身体末端的样子:

<script>
document.write('<script src=' +
('__proto__' in {} ? 'js/vendor/zepto' : 'js/vendor/jquery') +
'.js><\/script>')
</script>
<script src="js/foundation.min.js"></script>
<script src="js/foundation/foundation.js"></script>
<script src="js/foundation/foundation.alerts.js"></script>
<script src="js/foundation/foundation.clearing.js"></script>
<script src="js/foundation/foundation.cookie.js"></script>
<script src="js/foundation/foundation.dropdown.js"></script>
<script src="js/foundation/foundation.forms.js"></script>
<script src="js/foundation/foundation.joyride.js"></script>
<script src="js/foundation/foundation.magellan.js"></script>
<script src="js/foundation/foundation.orbit.js"></script>
<script src="js/foundation/foundation.placeholder.js"></script>
<script src="js/foundation/foundation.reveal.js"></script>
<script src="js/foundation/foundation.section.js"></script>
<script src="js/foundation/foundation.tooltips.js"></script>
<script src="js/foundation/foundation.topbar.js"></script>
<script>
$(document).foundation();
</script>

编辑2:解决方案

通过在头部包含jQuery来解决这个问题:

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Lorem Ipsum</title>
<link rel="stylesheet" href="css/foundation.min.css" />
<script src="js/vendor/jquery.js"></script>
<script src="js/vendor/custom.modernizr.js"></script>

实际的jQuery代码是相同的:

<script>
$(document).ready(function(){

    $("#important img").hover(function() {
      alert("");
    });

});
</script>
</body>

1 个答案:

答案 0 :(得分:2)

其他一些库可能会使用$,这意味着它可能会覆盖jQuery的$实例。因此,当您使用$时,它指的是另一个库,它似乎没有.hover()方法。

您可以使用jQuery代替$来引用jQuery库,也可以使用jQuery.noConflict();使jQuery库可以在另一个变量中访问 - http://api.jquery.com/jQuery.noConflict/

当然,请确保在页面上包含jQuery库:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

否则jQuery甚至不会被定义。