【问题标题】:Shopify, TypeError: $(...).jcarousel is not a functionShopify,TypeError:$(...).jcarousel 不是函数
【发布时间】:2013-02-13 01:42:00
【问题描述】:

所以我已经看到了具有相同标题的其他问题,并且我认为我没有遇到与他们相同的问题(特别是因为我没有使用 Drupal 并且文件似乎正在加载到页眉中如下所示)。我知道它找不到该功能,但我不知道为什么。这是 theme.liquid 头中的代码,用于设置 javascript 和样式:

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <link rel="canonical" href="{{ canonical_url }}" />

  {% assign maxmeta = 155 %}
  {% if template contains 'product' %}
  <meta name="description" content="{{ product.description | strip_html | strip_newlines | truncate: maxmeta | escape }}" />
  {% elsif template contains 'page' %}
  <meta name="description" content="{{ page.content | strip_html | strip_newlines | truncate: maxmeta | escape }}" />
  {% elsif template == 'index' and shop.description != '' %}
  <meta name="description" content="{{ shop.description }}" />
  {% endif %}


  {{ 'skinCustom.css' | asset_url | stylesheet_tag }}
  {{ 'jquery-1.4.2.min.js' | asset_url | script_tag }}
  {{ 'jquery.jcarousel.min.js' | asset_url | script_tag }}

  {% if template == 'index' %}
   <title>{{ shop.name }}</title>
  {% elsif template == '404' %}
    <title>Page Not Found | {{ shop.name }}</title>
  {% else %}
   <title>{{ page_title }} | {{ shop.name }}</title>
  {% endif %}

  {% include 'fb-open-graph' %}

  {% case settings.header_font %}
  {% when "Rancho, cursive" %}
    <link href='http://fonts.googleapis.com/css?family=Rancho' rel='stylesheet' type='text/css'>
  {% when "'Amatic SC', cursive" %}
    <link href='http://fonts.googleapis.com/css?family=Amatic+SC:700' rel='stylesheet' type='text/css'>
  {% when "'Sancreek', cursive" %}
    <link href='http://fonts.googleapis.com/css?family=Sancreek' rel='stylesheet' type='text/css'>
  {% when "'Ubuntu Condensed', sans-serif" %}
    <link href='http://fonts.googleapis.com/css?family=Ubuntu+Condensed' rel='stylesheet' type='text/css'>
  {% endcase %}

  {{ 'style.css' | asset_url | stylesheet_tag }}{% if template contains 'product' and settings.main_image_display == 'Lightbox' %}
  {{ 'jquery.fancybox-1.3.4.css' | asset_url | stylesheet_tag }}{% endif %}
  {{ 'modernizr.js' | asset_url | script_tag }}
  {{ 'option_selection.js' | shopify_asset_url | script_tag }}

  {% if template contains 'customers' %}
    {{ "shopify_common.js" | shopify_asset_url | script_tag }}
    {{ "customer_area.js"  | shopify_asset_url | script_tag }}
  {% endif %}

    <script type="text/javascript">

    $(document).ready(function() {
        $('#mycarousel').jcarousel({ vertical: true, scroll: 1, start: 3 });
    });

    </script>

  {{ '//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js' | script_tag }}

  {{ content_for_header }}

当页面呈现时,我得到以下信息(我编辑了与这个问题无关的额外内容)。我把箭头指向了 jcarousel 所依赖的 js 和样式页面的引用。在尝试将其移至 Shopify 项目之前,我在一个单独的文件中提供了一个示例。

<head>
  <meta charset="UTF-8">

  --> <link href="http://cdn.shopify.com/s/files/1/0212/5406/t/1/assets/skinCustom.css?241" rel="stylesheet" type="text/css"  media="all"  />
  --> <script src="http://cdn.shopify.com/s/files/1/0212/5406/t/1/assets/jquery-1.4.2.min.js?241" type="text/javascript"></script>
  --> <script src="http://cdn.shopify.com/s/files/1/0212/5406/t/1/assets/jquery.jcarousel.min.js?241" type="text/javascript"></script>


  <link href="http://cdn.shopify.com/s/files/1/0212/5406/t/1/assets/style.css?241" rel="stylesheet" type="text/css"  media="all"  />
  <script src="http://cdn.shopify.com/s/files/1/0212/5406/t/1/assets/modernizr.js?241" type="text/javascript"></script>
  <script src="http://cdn.shopify.com/s/shopify/option_selection.js?aaf2a68ed9671e345bbc7eef1e63563c51bde6eb" type="text/javascript"></script>



    <script type="text/javascript">

    $(document).ready(function() {
   ERROR HERE -->    $('#mycarousel').jcarousel({ vertical: true, scroll: 1, start: 3 });
    });

    </script>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>


<link rel="stylesheet" href="/global/theme-controls.css" type="text/css" />


</head>

我能想象的唯一可能发生的事情是其他脚本文件之一把它搞砸了。但是除了取出其他脚本文件之外,我不确定如何调试它。如果有人遇到类似的问题或对此有一些见解,我将不胜感激。除了不正确的实现(比如不考虑那个脚本),其他问题似乎都没有得到答案。我可能在做一些愚蠢的事情,但我已经研究过无数次了,我看不出它是什么。

谢谢,

艾伦

【问题讨论】:

  • 为什么要加载 jquery 两次?一个可能的问题是 jQuery 使用的美元符号。尝试用jQuery 替换它,这样你就有jQuery('#mycarousel') 或尝试使用jquery 的noconflict 模式。 api.jquery.com/jQuery.noConflict
  • 我看了很多遍……我想我需要第二双眼睛。问题就是,两次加载 jquery 会搞砸。我这样做是因为当我最初尝试使用 jcarousel 时,它说它找不到它所依赖的 jquery。所以我自己添加了它,只是假设它还没有加载它。感谢您的回复。我已经尝试了好几个小时。
  • 如果您的所有订单都正确并且没有冲突会发生什么。它仍然无法被识别(但您可以在控制台中看到它已加载。

标签: javascript jquery css shopify jcarousel


【解决方案1】:

我将上面的评论放入回复表单中。

这有两个问题。首先,您要加载 jQuery 两次。不要那样做。其次,jQuery 对$ 的使用可能会与其他javascript 库发生冲突,因此您应该使用jQuery 或使用jQuery 的noconflict mode.

【讨论】:

    【解决方案2】:

    正如@gaoshan88 所说,我加载了两次jquery。因为当我最初只添加 jcarousel.js 脚本时,它说没有 jquery 文件。所以我自己又加了一遍,不知道shopify还通过google.apis添加了最新版本。不太清楚为什么它首先用 jquery 给了我这个错误,但是当我取出 google.apis 脚本引用时它起作用了。感谢您的快速回复,我很感激。

    【讨论】:

    • 加载脚本时的顺序很重要。根据它们对其他脚本的依赖关系对它们进行排序。 jcarousel.js 文件应该加在 jquery.js 文件之后,因为 jcarousel 依赖于 jquery。视觉上很明显:$().jcarousel
    • 是的,这一定是问题所在,我以为我在第一次尝试时将 jcarousel 作为最后一个脚本加载了,但我猜想 google.apis 的 jquery 在它下面。
    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2021-06-05
    • 2019-10-03
    • 2017-03-06
    相关资源
    最近更新 更多