在我的select元素中添加一个类

时间:2015-09-30 15:57:30

标签: php wordpress

我只想添加" btn"到我的选择元素。所以select元素看起来像

<select class="btn">

在此代码中添加类:

<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">

        <?php
        $args = array(
            'show_option_none' => __( 'Select category' ),
            'show_count'       => 1,
            'orderby'          => 'name',
            'echo'             => 0,
        );
        ?>

        <?php $select  = wp_dropdown_categories( $args ); ?>
        <?php $replace = "<select$1 onchange='return this.form.submit()'>"; ?>
        <?php $select  = preg_replace( '#<select([^>]*)>#', $replace, $select ); ?>

        <?php echo $select; ?>

        <noscript>
            <input type="submit" value="View" />
        </noscript>

    </form>

2 个答案:

答案 0 :(得分:3)

documentation在您提供的参数中显示'class'选项

$args = array(
        'show_option_none' => __( 'Select category' ),
        'show_count'       => 1,
        'orderby'          => 'name',
        'echo'             => 0,
        'class'            => 'btn'
    );

答案 1 :(得分:0)

您还可以使用jquery添加类,如下所示: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("select").addClass("btn"); }); });