在Drupal 7中使用JQuery [英] Using JQuery in Drupal 7

查看:148
本文介绍了在Drupal 7中使用JQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的Drupal 7模块,并且喜欢在其中使用JQuery。

I'm writing my own Drupal 7 module, and like to use JQuery in it.

$('#field').toggle();

但是我收到这个错误:

TypeError: Property '$' of object [object DOMWindow] is not a function

似乎没有加载JQuery。否则$应该被定义。

It seems that JQuery is not loaded. Otherwise $ should be defined.

尽管我实际上包含在标题中:

Though I actually include it in the header:

<script type="text/javascript" src="http://rockfinder.de/misc/jquery.js?v=1.4.4"></script>

我必须做任何其他事情才能在Drupal中激活JQuery?是否被Drupal覆盖?

Do I have to do anything else to activate JQuery in Drupal? Is $ being overwritten by Drupal?

这是网站: http: //rockfinder.orgapage.de

推荐答案

从Drupal 7升级指南:


Javascript应该兼容
与jQuery之间的其他库

现有代码周围添加一个小包装:

Javascript should be made compatible with other libraries than jQuery by adding a small wrapper around your existing code:

(function ($) {
  // Original JavaScript code.
})(jQuery);

$全局将不再引用
的jquery对象。然而,使用这个
构造,本地变量$
将引用jquery,允许您的
代码通过$
访问jQuery,而代码不会

$全局的其他库冲突。

The $ global will no longer refer to the jquery object. However, with this construction, the local variable $ will refer to jquery, allowing your code to access jQuery through $ anyway, while the code will not conflict with other libraries that use the $ global.

您还可以使用'jQuery'变量而不是$代码中的$变量。

You can also just use the 'jQuery' variable instead of the $ variable in your code.

这篇关于在Drupal 7中使用JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆