0

我有这个代码:

<div id="archive-grid-view" class="row row-mobile">
  <div class="list box text-shadow">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <div class="col-xs-6 col-sm-4 col-md-3 list-item box archive-view-container grid">
        <a href="<?php the_permalink(); ?>">
          <div class="box-content">
            <img class="box-img" src="<?php the_field('postGridImage'); ?>" alt="<?php the_title();?>" title="<?php the_title();?>">
            <h5 class="title"><?php the_field('kitName'); ?></h5>
            <div class="archive-kit-info grid">
              <?php if ( has_term( 'none', 'countries' ) ) {} else { ?>
                <img class="country-flag" src="<?php bloginfo('stylesheet_directory'); ?>/images/country_flags/<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo $term->name; } ?> Flag.png" alt="><?php $term = get_field('country_of_origin');?><?php echo $term->name; ?> Flag">
              <?php } ?>
              <div class="label-feature scale CB-<?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
               <?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
              </div>
              <div class="label-feature manufacturer CB-<?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
               <?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
              </div>
              <div class="label-feature country CB-<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
                <?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
              </div>
              <?php if ( has_term( '', 'product_categories' ) ) { ?>
                <div class="label-feature product-categories <?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
                  <?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
                </div>
              <?php } else {}?>
              <?php if ( has_term( '', 'vehicle_categories' ) ) { ?>
                <div class="label-feature vehicle-categories <?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
                  <?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
                </div>
              <?php } else {}?>
              <div class="label-feature date">
                <?php echo the_time('m/d/Y h');?>
              </div>
            </div>
          </div>
        </a>
      </div>

    <?php endwhile; else: ?>

      <div class="page-header">
        <h1>Oh no!</h1>
      </div>

      <p>No content is appearing for this page!</p>

    <?php endif; ?>
  </div>
</div> <!-- Row Mobile -->

为了更改用户的布局,我创建了一个按钮来切换到列表样式布局。到目前为止,我有这段代码可以改变一些事情:

$("#list-view-button").click(function() {
    $( ".archive-view-container" ).each(function() {
        $( this ).removeClass().addClass("col-xs-12 list-item box archive-view-container list");
        $( ".country" ).show();
        $( ".product-categories" ).show();
        $( ".vehicle-categories" ).show();
        $( ".date" ).show();
        $( ".country-flag" ).hide();
        $( ".archive-kit-info" ).addClass( "list" ).removeClass( "grid" );
    });
});

现在我需要$( "archive-kit-info.list" ).nextAll().wrapAll( "<table>" );将 div 的所有直接子项包装archive-kit-info.list在一个表中并从那里工作,但是代码不起作用,我做错了什么?我是否还需要.each()在循环中添加?

当用户单击列表样式按钮时,我希望我的布局更改为:

<div id="archive-list-view" class="row row-mobile">
  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>Scale</th>
        <th>Manufacturer</th>
        <th>Country</th>
        <th>Product Category</th>
        <th>Vehicle Category</th>
        <th>Date</th>
      </tr>
    </thead>
    <tbody class="list box text-shadow">
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      <tr class="list-item box archive-view-container list">
        <td class="title">
          <?php the_field('kitName'); ?>
        </td>
        <td class="label-feature scale CB-<?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
          <?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
        </td>
        <td class="label-feature manufacturer CB-<?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
          <?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
        </td>
        <td class="label-feature country CB-<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
          <?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
        </td>
        <td class="label-feature product-categories <?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
          <?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
        </td>
        <td class="label-feature vehicle-categories <?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
          <?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
        </td>
        <td class="label-feature date">
          <?php echo the_time('m/d/Y h');?>
        </td>
      </tr>

      <?php endwhile; else: ?>

      <div class="page-header">
        <h1>Oh no!</h1>
      </div>

      <p>No content is appearing for this page!</p>

      <?php endif; ?>
    </tbody>
  </table>
</div>

这对 jQuery 可行吗?

4

2 回答 2

0

您的一般问题是您正在对具有类的所有元素进行操作,而不仅仅是.each迭代中当前 DIV 中的元素。您需要使用$(this).find()将自己限制在当前的 DIV 中:

$("#list-view-button").click(function() {
    $( ".archive-view-container" ).each(function() {
        $( this ).removeClass().addClass("col-xs-12 list-item box archive-view-container list");
        $(this).find( ".country" ).show();
        $(this).find( ".product-categories" ).show();
        $(this).find( ".vehicle-categories" ).show();
        $(this).find( ".date" ).show();
        $(this).find( ".country-flag" ).hide();
        $(this).find( ".archive-kit-info" ).addClass( "list" ).removeClass( "grid" )
            .nextAll().wrappAll("<table>");
    });
});

请注意,这会创建一个无效的 DOM —— 的子<table>元素必须是像<tbody>or这样的表格元素<tr>。您需要将所有 DIV 转换为行,并将其内容转换为<tr>元素。

于 2014-09-20T20:57:50.283 回答
0

CSS您可以使用(请参阅下面的代码段)实现类似表格的结构。您只需要show(),hide()和元素theadtfoot只需将类更改为适当的div元素。

例如,基于下面的示例,我们可以编写像表格行一样的元素:

$( ".archive-view-container" ).each(function() {
    $( this ).removeClass().addClass("list-item box archive-view-container list");
    ...
    $(".thead").show();
    $(".thfoot").show();
    ...
});
.list-item.box.archive-view-container.list {
    display: table-row
}

.table {
  display: table
}

.tr {
  display: table-row
}

.thead {
  display: table-header-group
}

.tbody {
  display: table-row-group
}

.tfoot {
  display: table-footer-group
}

.col {
  display: table-column
}

.colgroup {
  display: table-column-group
}

.td,
.th {
  display: table-cell;
  height: 2rem;
  font-size: 1rem;
  padding: 1rem 2rem 0 2rem;
  white-space: nowrap;
}
.td {
  background-color: #0FF;
}
.th, .tfoot .td {
  background-color: #F0F;
  font-weight: 700;
  color: #fff;
}
.caption {
  display: table-caption
}
<div class="archive-kit-info list table">
  <div class="thead">
    <div class="tr">
      <div class="th">Name</div>
      <div class="th">Scale</div>
      <div class="th">Manufacturer</div>
      <div class="th">Country</div>
      <div class="th">Product Category</div>
      <div class="th">Vehicle Category</div>
      <div class="th">Date</div>
    </div>
    <div>
    </div>
    <div class="tr">
      <div class="td">
        Car 1
      </div>
      <div class="td">
        10
      </div>
      <div class="td">
        Manu1
      </div>
      <div class="td">
        Greenland
      </div>
      <div class="td">
        Automobiles
      </div>
      <div class="td">
        Sport Cars
      </div>
      <div class="td">
        2019
      </div>
    </div>
  </div>
  <div class="tfoot">
    <div class="td">Name</div>
    <div class="td">Scale</div>
    <div class="td">Manufacturer</div>
    <div class="td">Country</div>
    <div class="td">Product Category</div>
    <div class="td">Vehicle Category</div>
    <div class="td">Date</div>
  </div>
</div>

于 2019-04-20T08:48:47.473 回答