Warning: Table './drupal_fandt_old/accesslog' is marked as crashed and last (automatic?) repair failed query: SELECT uid, timestamp FROM accesslog WHERE (path = 'node/142' OR uid > 0) AND sid = 'e0423472a5a1e2b9a4aa58d0b3a89fda' ORDER BY timestamp DESC LIMIT 0, 1 in /var/www/akishige/fandt/includes/database.mysqli.inc on line 128
Drupal 7 における jQuery | FOREST & trees

Drupal 7 における jQuery

Drupal 7 では jQuery の振る舞いがちょっと Drupal 6 のそれとは異なるようで、少し手こずりました。

まず、通常 Drupal.behaviors で jQuery の全コードを囲むと思いますが、Drupal 6 では


Drupal.behaviors.module_name = function(context) {
  your code goes here...
};

となっていたことと思いますが、Drupal 7 では


Drupal.behaviors.module_name = {
  attache: function(context) {
    your code goes here...
  }
};

という風に変わります(参照:Converthing 6x modules to 7.x - Changed Drupal.behaviors ...

また、通常 jQuery では、以下のように $(ドルサイン)を使ってコマンドを書き始めます。


$(document).ready(function() {
  alert("Hello World");
};

しかし、Drupal 7 に含まれる jQuery では、この $ マークで jQuery コマンドを書き始めるということができないようです。

この問題を解決するには2通りの方法があります。1つ目は、$ の代わりに jQuery という文字列を用いるというものです。


jQuery(document).ready(function() {
  alert("Hello World");
};

もう一つは、以下のように jQuery のコード全体を囲う、というものです。(参考:Converting 6.x modules to 7.x - JavaScript should be compatible with other libraries than jQuery


(function($) {
  $(document).ready(function() {
    alert('Hello World.');
  })
})(jQuery)

以上をまとめると、Drupal 7 向けに jQuery を使うには、以下のようにするのが基本、と言えそうです。(コメント文からした3行は任意に書き換える)


Drupal.behaviors.your_module = {
  attach: function(context) {
    (function($) {
      // your code goes here ...
      $(document).ready(function() {
        alert('Hello World.');
      })
    })(jQuery)
  }
}

Comments

mcafee.com/activate

Mcafee.com/activate Online Help  – Step by Step guide for McAfee Activate, Download & complete installation online. We are providing independent support service if in case you face problem to activate or Activate McAfee product. Just fill the form below and will get in touch with you as quick as possible.

mail.aol.com

my.norton.com

my.norton.com – Norton is very popular for its extensive security tools. To fulfill the need of every user, Norton has designed various versions of security software tools. These versions of tools are Norton Internet Security.

norton.com/setup

Norton.com/setup Online Help – Step by Step guide for Norton Setup, Download & complete installation online. We are providing independent support service if in case you face problem to activate or Setup Norton product.

João Paulo

O Plugin Hotlink Plus está sendo a salvação para muitos afiliados que anunciam no facebook ads, isso porque ele tem várias funcionalidades que hoje são praticamente necessárias para se trabalhar com anúncio pago, principalmente face ads.Hot Links Plus é um plugin Premium para WP, com diversos recursos e Multi-Funções O Plugin Hot Links PLUS é o mais poderoso e mais completo plugin de gerenciamento de links do mercado, já usado e aprovado por grandes nomes do marketing nacional.

non-slip surface area or

non-slip surface area or remodeling a preexisting pool to be able to freshen the particular image,kissanime | gogoanime

Those who come to read your

Those who come to read your article will find lots of helpful and informative tips. This is really a pleasant and useful, containing all data furthermore greatly affect the new innovation. Much obliged for sharing it
Avriq,Avriq IndiaI am looking to learn more about this issue. Thank you for sharing. I will follow this site regularly

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.