Revision of Drupal7 表單內載入 css, js from Mon, 2012-11-05 00:19

Drupal7 中以下的代碼是不會達到你想要的效果的

<?php
function hook_form_alert(&$form, &$form_states) {
 
drupal_add_js(drupal_get_path('module', 'example') . "/js/example.js");
}
?>

hook_form_alter 中使用 drupal_add_css() drupal_add_js() 是沒有效果的

正確的做法是 form 的 preprocess 或者使用 form API 的 #attach:

<?php
$form
['#attached']['css'] = array(
 
drupal_get_path('module', 'ajax_example') . '/ajax_example.css',
);

$form['#attached']['js'] = array(
 
drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);
?>

ref:
http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.ht...

Google