Using JQuery in Drupal 7

前端 未结 5 626
长情又很酷
长情又很酷 2021-01-30 17:18

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

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

But I\'m getting this error:

TypeErr         


        
5条回答
  •  时光取名叫无心
    2021-01-30 18:04

    From the Drupal 7 upgrade guide:

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

    (function ($) {
      // Original JavaScript code.
    })(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.

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

提交回复
热议问题