0

所以我创建了一个类,例如:

class CoreTheme_Form_Helpers_TabbedForm extends AisisCore_Form_Helpers_Content{

    protected $_html = '';

    public function init(){
        parent::init();

        if(isset($this->_options)){
            $this->_html .= '<ul class="nav nav-tabs">';
            foreach($this->_options as $options){
              $this->_html .= '<li><a href="#'.str_replace(" ", "", $options['tab']).'" data-toggle="tab">
                '.$options['tab'].'</a></li>';
            }
            $this->_html .= '</ul>';

            $this->_html .= '<div class="tab-content">';
            foreach($this->_options as $options){
                $this->_html .= '<div class="tab-pane" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
            }
            $this->_html .= '</div>';           
        }
    }

    public function __toString(){
        return $this->_html;
    }
}

我要做的是使用类选项卡窗格将活动类添加到 div 中,但只添加一次,并且只添加到第一个。

所以在:

foreach($this->_options as $options){
    $this->_html .= '<div class="tab-pane" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
}

html应该是这样的:

<div class="tab-pane active" id="something">...</div>
<div class="tab-pane" id="something1">...</div>
<div class="tab-pane" id="something2">...</div>

想法?

4

5 回答 5

1

只需使用一个变量来存储额外的类active并在第一次设置后将其重置。

$extraClass = "active";
foreach($this->_options as $options){
    $this->_html .= '<div class="tab-pane '.$extraClass. '" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
    $extraClass = "";
}
于 2013-02-13T18:08:10.977 回答
1

如果您不介意从客户端添加它,这是一个 jQuery 单线:

$('.tab-pane').first().addClass('active');
于 2013-02-13T18:08:32.150 回答
0

如果您需要计数器,为什么要使用foreach?使用常规for

for ($i=0 ; $i < count($this->_options) ; $i++){

    $options = $this->_options[$i];

    if ($i==0) {
        // do something else
    }
于 2013-02-13T18:08:33.517 回答
0
$active = " active";
foreach($this->_options as $options){

    $this->_html .= '<div class="tab-pane'. $active .'" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
    $active = "";

}

我认为它可以工作

于 2013-02-13T18:08:54.127 回答
0

你可以做一个 str_replace 来替换整个:

str_replace('tab-pane', 'tab-pane active', $this->_html, 1);

...其中最后一个参数为 1,是您替换它的次数。 http://php.net/manual/en/function.str-replace.php

于 2013-02-13T18:11:03.583 回答