在 Drupal 7 中使用 JQuery [英] Using JQuery in Drupal 7
问题描述
我正在编写自己的 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
推荐答案
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);
$ global 将不再引用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屋!