主题元素中附加JS/CSS

主题元素中附加JS/CSS

(issue) Individual elements now have the ability to define what JavaScript and cascading stylesheets are associated with them. This is stated in the #attached_js and #attached_css property.

Drupal 6.x:

<?php
function example_admin_settings() {
 
// Add example.admin.css
 
drupal_add_css(drupal_get_path('module', 'example') .'/example.admin.css');
 
// Add some inline JavaScript
 
drupal_add_js('alert("You are visiting the example form.");', 'inline');
 
// Add a JavaScript setting.
 
drupal_add_js(array('mymodule' => 'example'), 'setting');
 
$form['example'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('Example');
  );
  return
$form;
}
?>

Drupal 7.x:

<?php
function example_admin_settings() {
 
$form['#attached_css'] = array(
   
// Add example.admin/css.
   
drupal_get_path('module', 'example') . '/example.admin.css'
 
),
 
$form['#attached_js'] = array(
   
// Add some inline JavaScript.
   
'alert("You are visiting the example form.");' => 'inline',
   
// Add a JavaScript setting. Note that when the key is a number, the 'data' property will be used.
   
array(
     
'data' => array('mymodule' => array(...)),
     
'type' => 'setting'
   
),
  );
 
$form['example'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('Example');
  );
  return
$form;
}
?>

Syndicate content