dsuoedtom207012191 2013-02-13 18:05
浏览 203
已采纳

如何在foreach循环中的一个div中添加一个类

So I crated a class such as:

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;
    }
}

What I am looking to do is add the class active to the div with the class tab-pane, but only once, and only to the first one.

so in:

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

the html should be something like:

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

ideas?

  • 写回答

5条回答 默认 最新

  • doukui4836 2013-02-13 18:08
    关注
    $active = " active";
    foreach($this->_options as $options){
    
        $this->_html .= '<div class="tab-pane'. $active .'" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
        $active = "";
    
    }
    

    I think it can works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?