【问题标题】:Uncaught TypeError: undefined is not a function (jQuery & jQuery Easing)Uncaught TypeError: undefined is not a function (jQuery & jQuery Easing)
【发布时间】:2014-07-24 00:45:59
【问题描述】:

我正在建立一个网站。我建立了后端,现在我正在做前端。在前端,我有引导程序、各种字体很棒的图标和 angular。还应注意这是一个 Rails 应用程序。

我想使用 jQuery 和 jQuery 缓动添加 1 个页面滚动功能,该功能由一个很棒的字体箭头提供支持。我显然没有做对,因为当使用它时它变成蓝色(认为它可能是由于href的“活动状态”?),并将我页面上的所有字体(一切)变成蓝色。

在 chrome 的控制台中,我得到了错误;

Uncaught TypeError: undefined is not a function

在jQuery中指的是这一行(可能是任意的);

this.pos = eased = jQuery.easing[ this.easing ](

这是我的其余代码;

index.html.erb

<div class="page-scroll" id="arrow"><a href="#clients"><i class="fa fa-chevron-down"></i><a></div>

styles.css

  #arrow {
    font-size: 25px;
    color: grey;
}

ma​​in.js

    $(function() {
      $('.page-scroll a').bind('click', function(event){
         $('html, body').animate({
             scrollTop: $("#clients").offset().top
         }, 1500);
         event.preventDefault();
     });
  });

application.html.erb(感谢另一个 SO 问题,jQuery 和 jQuery Easing 放置在样式表下)

<!-- Easing for Page Scroll -->
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>

当我注释掉索引中的 div 时,所有字体都会恢复正常(处于活动状态时为蓝色)。当我单击按钮时,它会将我带到我想要的元素,但它会将你放在那里,它没有缓动方面(这就是我添加缓动的原因)。

我已经搜索了相当多的内容,对此我感到困惑(尤其是文本的更改)。任何帮助将不胜感激。

【问题讨论】:

  • 您可以在控制台中访问这些变量来隔离问题吗? (window.pos, eased, JQuery.easing, window.easing) 也尝试调试该行并在抛出异常之前检查变量的状态
  • 您没有正确关闭 标记。它缺少/ 字符
  • @jasonscript 谢谢!我关闭了它,它负责字体。
  • @LinuxN00b 在控制台中,我可以访问 jQuery.easing。 window.pos 和 window.easing 都显示 undefined。
  • 啊,好吧,在我看来这是一个上下文问题,我认为 window.easing 和 window.pos 应该附加到元素而不是窗口。你有一些狡猾的上下文绑定或调用吗?有没有更相关的代码?

标签: javascript jquery css ruby-on-rails-4 jquery-easing


【解决方案1】:

我已经弄清楚是什么原因造成的。

发生这种情况时,即使正在调用 jQuery,您的页面也无法找到它。要解决此问题,请确保您的页面内容看起来像这样(对于 rails,所有这些都将在 application.html.erb 中);

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Good SEO.">
    <meta name="author" content="">

    <title>Your Title</title>

    <!-- Bootstrap Core CSS for smaller projects/single page I'll use a CDN -->
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css">

    <!-- Fonts -->
    <link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel='stylesheet' type='text/css'>
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet" type='text/css'>

    <!-- CSS or in assets/stylesheets-->
    <link href="/assets/styles.css" media="screen" rel="stylesheet">

</head>

关闭 body & html 标签后

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js">
</body>    
</html>

我注意到顺序必须是 jQuery,bootstrap(如果您正在使用它),然后是 jquery.easing。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多