首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按类名为块子类添加/删除类

按类名为块子类添加/删除类
EN

Stack Overflow用户
提问于 2021-05-06 15:40:20
回答 1查看 47关注 0票数 1

我有这个html来展示我的产品。

代码语言:javascript
复制
<div class="row section-margin">
            <!-- Single Product Tab Start -->
            <div class="col-lg-12 col-custom single-product-tab">
                <ul class="nav nav-tabs" id="myTab" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link text-uppercase contact-tab" data-bs-toggle="tab" href="#connect-1"
                           role="tab" aria-selected="false">Характеристики</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-uppercase contact-tab" data-bs-toggle="tab" href="#connect-2"
                           role="tab" aria-selected="false">Политика доставки</a>
                    </li>
                </ul>

                <div class="product-block-information">
                    <div class="product-info">
                        <table class="table table-hover">
                            <tbody>
                                <tr>
                                    <th scope="row">Объем двигателя</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->engine_volume ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Тип двигателя</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->engine_type ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Охлождение</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->cooling ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Макс. мощность</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->max_power ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Макс. крутящий момент</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->max_engine_speed ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Сиcтема питания</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->supply_system ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Система зажигания</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->ignition_system ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Система пуска</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->launch_system ?></th>
                                </tr> <tr>
                                    <th scope="row">КПП / Главная передача</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->kpp ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Передняя подвеска</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->front_suspension ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Задняя подвеска</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->ear_suspension ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Тормоза, передний / задний</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->brakes ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Шины, передняя / задняя</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->tires ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">ДхШхВ</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->dshv ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Колесная база</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->wheelbase ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Высота по сидению</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->seat_height ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Клиренс</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->ground_clearance ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Сухой вес</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->dry_weight ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Обьем топливново бака</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->fuel_tank_volume ?></th>
                                </tr>
                                <tr>
                                    <th scope="row">Максимальная скорость</th>
                                    <th scope="row"><?php  echo $model->productsAttributes->maximum_speed ?></th>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="product-info">
                        Text politici dostawci
                    </div>
                </div>
                <!-- Single Product Tab End -->
            </div>
        </div>

我有这个jquery

代码语言:javascript
复制
//show product information
    jQuery('.nav-item').click(function (e) {
      const child = jQuery(this).index();
      console.log(child);
        jQuery('.product-block-information:nth-child(1)').addClass('info-active');
      //info-active
    })

当用户单击li元素上的第一个子元素时,将class info-active添加到product-block-information上的第一个元素中,当用户在li上单击第二个子元素时,从第一个元素中删除info-active并在product-block-information上将info-active类添加到第二个子元素中。

怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-07 05:54:32

您可以使用index() & :eq()方法来实现这一点。

演示代码

代码语言:javascript
复制
jQuery('.nav-item').click(function(e) {
  const child = jQuery(this).index();
  //remove info active from others..
  jQuery('.product-block-information > .product-info').removeClass("info-active")
  //add to where index matches
  jQuery('.product-block-information > .product-info:eq(' + child + ')').addClass('info-active');
})
代码语言:javascript
复制
.info-active {
  color: green;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row section-margin">
  <!-- Single Product Tab Start -->
  <div class="col-lg-12 col-custom single-product-tab">
    <ul class="nav nav-tabs" id="myTab" role="tablist">
      <li class="nav-item">
        <a class="nav-link text-uppercase contact-tab" data-bs-toggle="tab" href="#connect-1" role="tab" aria-selected="false">Характеристики</a>
      </li>
      <li class="nav-item">
        <a class="nav-link text-uppercase contact-tab" data-bs-toggle="tab" href="#connect-2" role="tab" aria-selected="false">Политика доставки</a>
      </li>
    </ul>

    <div class="product-block-information">
      <div class="product-info">
        <table class="table table-hover">
          <tbody>
            <tr>
              <th scope="row">Объем двигателя</th>
              <th scope="row">
                <?php  echo $model->productsAttributes->engine_volume ?>
              </th>
            </tr>

          </tbody>
        </table>
      </div>
      <div class="product-info">
        Text politici dostawci
      </div>
    </div>

  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67421501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档