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 = '3656a4f64a01d2293df7d1fbd87accd1' 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

walatra berry jus walatra

I thank you for the

I thank you for the information! I was looking for and could not find. You helped me!asp

It is a very nice article

It is a very nice article including a lot of viral content. I am going to share it on social media. Get the <a href="http://www.chennaicrackersonline.com/fireworks-online">fireworks online / crackers online </a> in chennai.

I thank you for the

I thank you for the information! I was looking for and could not find. You helped me!alternative ccleaner

Thank you for sharing in this

Thank you for sharing in this article Obat Perangsang Wanitaand Obat Perangsang Wanita AlamiI can learn a lot Obat Perangsang Wanita Murah I can learn a lot and could also be a referenceand could also be a reference obat PERANGSANG wanitaI recommend any content. Wanita Obat PerangsangIt's always wonderful to work out you will explain in words from middle obatlibido.comand additionally picture quality using this valuable content is quite simply recognized. ”obat perangsang wanita” much more information access kunjungi websiteand thanks you

Thank you for your work on

Thank you for your work on the blog! You're doing a good job!itunes alternative

Thanks for the useful

Thanks for the useful information! You helped me with advice!192.168.o.1

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.