问题描述
我正在编写自己的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?
这是网站:
推荐答案
(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!