Uso de jQuery en Drupal 7


Estoy escribiendo mi propio módulo Drupal 7, y me gusta usar jQuery en él.

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

Pero estoy recibiendo este error:

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

Parece que jQuery no está cargado. De lo contrario should debería definirse.

Aunque en realidad lo incluyo en el encabezado:

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

¿Tengo que hacer algo más para activar jQuery en Drupal? ¿Está siendo sobrescrito por Drupal?

Ese es el sitio web: http://rockfinder.orgapage.de

Author: JochenJung, 2011-01-13

4 answers

De la guía de actualización de Drupal 7:

Javascript debe ser compatible con otras bibliotecas que jQuery por agregar una pequeña envoltura alrededor de su código existente:

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

El global global ya no se referirá a el objeto jquery. Sin embargo, con esto construcción, la variable local $ se referirá a jquery, permitiendo su código para acceder a jQuery a través de j de todos modos, mientras que el código no conflicto con otras bibliotecas que usan el $ global.

También puede usar la variable 'jQuery' en lugar de la variable $ en su código.

 89
Author: Eaton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-13 14:54:34

De acuerdo con Firebug, su archivo jQuery se está cargando:

texto alt

Pero el $ está siendo sobrescrito por otra cosa:

texto alt


Lo que debe hacer es encapsular el uso de la variable $ con una función que se invoca a sí misma usando el objeto jQuery como primer argumento real:

(function ($) {

 // in this function, you can use the $ which refers to the jQuery object

}(jQuery));
 14
Author: Andreas Grech,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-13 14:59:13

Es probable que su script no se inicialice de esta manera, tendrá que usar Drupal.comportamiento.SU nombre

(function ($) {
Drupal.behaviors.YOURTHEMENAME = {
attach: function(context, settings) {

/*Add your js code here*/
alert('Code');

}

};
})(jQuery);    
 8
Author: Guus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-22 12:16:38

"$ no es una función" es un error muy común que puede enfrentar mientras trabaja con jQuery. Usted puede intentar cualquier respuesta de dado abajo:

(function($){
//your can write your code here with $ prefix
})(jQuery);

O

jQuery(document).ready(function($){
//Write your code here
});

Básicamente esto permitirá que nuestro código se ejecute y use el acceso directo $ para jQuery.

 0
Author: Haripal Rao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-08 17:34:18