自定义模块:未捕获TypeError:undefined不是函数

时间:2014-12-28 20:48:36

标签: javascript jquery drupal module drupal-7

我正在尝试编写自己的Drupal模块,并且我想要包含Javascript。我尝试包含一个非常简单的jquery文件,但我一直得到Uncaught'TypeError:undefined不是一个函数'。 我已经更新了Drupal的jQuery版本。

供参考,这些是我正在使用的脚本。

我的.info文件

name = Test
description = A slideshow that implements the PgwSlider Javascript.
core = 7.x

我的.module文件     

 /**
 * Implements hook_help().
 *
 * Displays help and module information.
 *
 * @param path 
 *   Which path of the site we're using to display help
 * @param arg 
 *   Array that holds the current path as returned from arg() function
 */
function test_help($path, $arg) {
  switch ($path) {
    case "admin/help#pgwslider":
      return '<p>' . t("Displays a slideshow") . '</p>';
      break;
  }
}

function test_init() {
    drupal_add_css(drupal_get_path('module', 'test') .'/styles.css');
    drupal_add_js(drupal_get_path('module', 'test') .'/test.js');
}

我的.js文件

$(document).ready(function() {
    $('p').click(function(){
        $(this).hide();
    });
    console.log('test')
});

有什么想法吗?

第二,较小的问题:

这是包含js文件的最佳方式吗?我知道在主题中这是在.info文件中完成的,但是对于模块,我没有找到全面的答案。

谢谢一群人!

1 个答案:

答案 0 :(得分:3)

假设.js文件的路径正确,您必须阅读一些standards of Javascript code

所有的JS代码都必须声明如下:

/**
 * @file
*/
(function ($) {
  "use strict";

  // Your js code

})(jQuery);

您添加JS的方式已经足够了,但我建议您使用hook_library()

问候。