dongtao4319 2017-05-02 01:57
浏览 17
已采纳

如何基于链接动态显示php内容

I would like to replicate this site's glossary function: https://www.mortgageloan.com/finance-glossary-terms

Objective: Click on a letter and content corresponding to that letter will be shown. Click on another letter, and only the clicked letter's content is shown.

I have my dictionary.php file with an array setup like this:

$dictionary = array(
    'A'=>array(
        'Term1'=>'Definition1',
        'Term2'=>'Definition2',
    ),
    'B'=>array(
        'Term1'=>'Definition1',
        'Term2'=>'Definition2',
    ),
);

I can display the Terms and Definitions fine with a foreach loop, but it displays all the terms and definitions. How can I display the terms and definitions pertaining to only their corresponding letters when the user clicks on that letter?

  • 写回答

2条回答 默认 最新

  • dongqiao9394 2017-05-02 03:14
    关注

    The site you linked doesn't seem to use AJAX like @JeremyE suggests. It just loads a bunch of "tabs" (one for each letter) and then lets you switch between them as demonstrated here. It still loads all the data at once, just hides parts.

    The Menu Bar

    This loops through each letter in $dictionary and adds it as a button

    <div>
        <?php foreach($dictionary as $letter => $terms) { ?>
            <button onclick="openTab('<?= $letter ?>')"><?= $letter ?></button>
        <?php } ?>
    </div>
    

    The Tabs:

    This generates a tab for each letter in $dictionary and then creates a paragraph for each term within that.

    <?php foreach($dictionary as $letter => $terms) { ?>
    
        <div class="tab" id="<?= $letter ?>" style="display: none;">
    
            <?php foreach($terms as $term => $def) { ?>
                <p><?= $term . " is: " . $def;?></p>
            <?php } ?>
    
        </div>
    <?php } ?>
    

    JS to add functionality

    This JS function simply shows a tab with a certain ID, and hides all the others. It's called by the buttons inside the Menu.

    <script>
    function openTab(tab) {
        var i;
        var x = document.getElementsByClassName("tab");
    
        for (i = 0; i < x.length; i++) {
            x[i].style.display = "none";
        }
    
        document.getElementById(tab).style.display = "block";
    }
    </script>
    

    This gives you the basic structure of how it's done.

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

报告相同问题?

悬赏问题

  • ¥15 按颜色进行点云分割-python
  • ¥15 Matlab如何实现汽车变道切入场景的批量仿真
  • ¥20 COMSOL频域研究,要加法向加速度的话,加速度随时间变化该如何设置?
  • ¥15 无刷电机要通过什么才能做各种动作
  • ¥15 VS2010通过OnCtlColor函数改变radio控件字体的颜色为白色,但是程序运行后发现不起作用,怎么办?
  • ¥15 Qt中实现子线程和管理线程类之间实时通信
  • ¥15 cacls 命令如何解除锁定文件夹?
  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 Vue3重构Nuxt3,出现客户端数据和服务端数据不一致的情况
  • ¥15 如何在sql server里完成筛选