【问题标题】:Add css property for individual components in form elements - Drupal为表单元素中的单个组件添加 css 属性 - Drupal
【发布时间】:2011-02-16 09:07:20
【问题描述】:

我有一个 drupal 表单,我需要向表单元素中的组件添加单独的类。

也就是说,我需要将属性 class='text' 添加到 'textbox(实际框)' 和 class='title' 到 'textbox title' (#title')。

我该怎么做?

【问题讨论】:

    标签: drupal drupal-forms


    【解决方案1】:

    对于第一种情况——将类添加到文本字段本身——使用#attributes 属性:

    $form['foo'] = array(
      '#type' => 'textfield',
      '#title' => t('Textbox title'),
      '#attributes' => array(
        'class' => array('text'), // change to just 'text' for Drupal 6
      ),
    );
    

    对于第二种情况——将类添加到标签中——你对 Forms API 不走运。相反,您可以使用字段和标签的包装类来定位它:

    .form-item-field-foo label {
      /* CSS here */
    }
    

    【讨论】:

    • 注意D7中class的内容必须是数组,"'class' => array('text'),"
    【解决方案2】:

    已经有人回答了您的问题Remove class=“form-item” from Drupal forms
    该功能可以将 class='title' 添加到标题中。
    但是对于 class='text' 你应该使用 theme_textfield(或 theme_textarea)来拥有 template.php
    从 ./included/form.inc
    获取代码 但我建议对所需的自定义表单进行主题化,而不是所有表单元素。它可能会在某个地方引发样式冲突等。当然,如果客户不要求更改所有表单元素。在实际情况下不需要更改默认元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多