javascript - 尝试在 head 标记中添加hammer.js 和其他库
问题描述
我的 header.php 文件看起来像这样,但当我尝试使用hammer.js 时它不起作用。我也遇到了 jquery.hammer.customswipe 和 events.js 的错误。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/jquery-scrollto.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/scripts.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/core/js/libraries/jquery.hammer.min.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/core/js/functions/jquery.hammer.customswipe.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/scripts/core/js/events.js"></script>
错误:
jquery.hammer.js:8 Uncaught ReferenceError: Hammer is not defined
at jquery.hammer.js:8
at jquery.hammer.js:10
(anonymous) @ jquery.hammer.js:8
(anonymous) @ jquery.hammer.js:10
jquery.hammer.customswipe.js:123 Uncaught TypeError: options.containers.hammer is not a function
at Object.move (jquery.hammer.customswipe.js:123)
at Object.left (jquery.hammer.customswipe.js:42)
at init (jquery.hammer.customswipe.js:157)
at init.$.fn.customSwipe (jquery.hammer.customswipe.js:173)
at events.js:2
at events.js:10
move @ jquery.hammer.customswipe.js:123
left @ jquery.hammer.customswipe.js:42
init @ jquery.hammer.customswipe.js:157
$.fn.customSwipe @ jquery.hammer.customswipe.js:173
(anonymous) @ events.js:2
(anonymous) @ events.js:10
解决方案
显示您提到的错误会很有用。但是,话虽如此,我会转而使用wp_enqueue_script
WordPress Enqueue Script。加载文件而不是直接将它们添加到 header.php 文件中。
您可能还必须移动这些文件以加载到页脚而不是页眉中。一个例子是。
wp_enqueue_script( 'hammerJS', get_template_directory_uri() . '/scripts/core/js/libraries/jquery.hammer.customswipe.js', ['jquery'], false, true );
这将确保在此脚本之前加载 jquery(['jquery'] 参数)并将脚本放在页脚(最后一个参数)中。
尝试一下,看看你是否能得到任何结果,但显示你遇到的错误将使我们能够提供更多帮助。
推荐阅读
- javascript - 无法将 asp.net mvc 会话值传递给 javascript 变量
- sequelize.js - 续集独特的复合材料失败
- css - 简写 flex 和 flex-basis 属性之间的区别
- flutter - 如何扩展“冻结”抽象类
- react-native - 使用 React Native 沿线或路径为 SVG 对象设置动画
- javascript - 在我的新 div 中创建的 angular 指令加载组件而不是在外部
- r - 函数 read_excel() 在一个文件中解释“货币”数据类型完全错误,而在另一个文件中解释为好
- javascript - 问:使用 DOM 将 Javascript(通过链接到 HTML 文件)添加到自定义元素
- javascript - 纱表,拿不到/
- php - 由于网络爬虫导致的过度文件系统访问似乎破坏了容器文件系统