You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用单个按钮切换WordPress动态生成表格行的显示与隐藏

优化后实现方案

步骤1:修改PHP/HTML模板代码

首先替换原有表格渲染和按钮部分的代码,给超出3行的表格行统一加extra-row类,同时把两个按钮替换为单个切换按钮:

<table width="100%" cellspacing="10" cellpadding="10" class="tablec">
    <thead>
        <tr> 
            <th><strong>平面图</strong></th>
            <th><strong>尺寸</strong></th>
            <th><strong>价格</strong></th>
        </tr>
    </thead>
    <tbody>
    <?php 
    function RemoveSpecialChar($str)
    {
        $res = str_replace( array( '\'', '"',
        ',' , ';', '<', 'West', 'Floor Plan', '>' , '-' ), ' ', $str);
        return $res;
    }

    $i = 0; // 初始化计数
    foreach( $floor_plans as $plans ) { 
        $i++; 
        if($plans['fave_plan_title'] == "Master Plan"){
            break;
        }
        // 超出3行的行加extra-row类
        $row_class = $i > 3 ? 'extra-row' : '';
        ?>
        <tr class="<?php echo $row_class; ?>">
            <td> 
                <a href="<?php echo esc_url( $plans['fave_plan_image'] ); ?>" data-lightbox="roadtrip">
                    <img class="borderr" src="<?php echo esc_url( $plans['fave_plan_image'] ); ?>" alt="<?php echo $plans['fave_plan_description']; ?>" width="100" height="100" title="<?php echo $plans['fave_plan_description']; ?>">
                </a>
            </td>
            <td>
              <?php 
                $str=ltrim(chop($plans['fave_plan_description'],"Floor Plan"), get_the_title());
                $str1 = RemoveSpecialChar($str);
                echo $str1;
              ?> <br>
              <b><?php echo esc_attr( $plans['fave_plan_size'] ); ?> 平方英尺</b>
            </td>
            <td><button class="btn btn-primary">获取报价</button></td>
        </tr>
        <?php } ?>
    </tbody>
</table> <br>
<div class="wrapperr">
    <!-- 单个切换按钮 -->
    <button id="togglePlanBtn" class="btn btn-primary">
        显示更多 <i class="fa fa-arrow-down" style="font-size:14px"></i>
    </button>
</div>
</div><!-- block-content-wrap -->
</div><!-- block-wrap -->
</div><!-- property-floor-plans-wrap -->
<?php } ?>

步骤2:新增CSS规则(推荐放到主题的自定义CSS里)

实现初始状态隐藏多余行,避免页面加载闪烁:

.extra-row {
    display: none;
}

步骤3:替换原有JS代码

删掉原来所有硬编码的行隐藏逻辑,替换为以下toggle逻辑:

document.addEventListener('DOMContentLoaded', function() {
    const toggleBtn = document.getElementById('togglePlanBtn');
    const extraRows = document.querySelectorAll('.extra-row');
    let isExpanded = false; // 记录当前展开状态

    toggleBtn.addEventListener('click', function() {
        if (!isExpanded) {
            // 展开所有多余行
            extraRows.forEach(row => row.style.display = 'table-row');
            toggleBtn.innerHTML = '收起 <i class="fa fa-arrow-up" style="font-size:14px"></i>';
        } else {
            // 隐藏多余行
            extraRows.forEach(row => row.style.display = 'none');
            toggleBtn.innerHTML = '显示更多 <i class="fa fa-arrow-down" style="font-size:14px"></i>';
        }
        isExpanded = !isExpanded;
    });
});

优化点说明

  • 完全适配动态生成的任意数量表格行,不需要硬编码行ID,新增/减少数据不用改代码
  • 用CSS实现初始隐藏,避免JS加载前的行闪烁问题,大幅降低客户端性能消耗
  • 单按钮逻辑精简,代码量比原来减少80%以上,维护成本更低

内容的提问来源于stack exchange,提问作者user3787062

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 17:54:10