doufen1890 2014-09-18 21:41
浏览 21
已采纳

jQuery显示并隐藏PHP生成的内容

I am a total jQuery noob. But I need to use it for a script in combination with PHP. The idea is that people can add a new lawyer on the backend and that a button and content are added to the page. On the site you would then be able to cycle through the lawyers by using the buttons as in the example below.

http://strak-design.com/varlaw/team

Here is the site in question and how I would like it to work. Right now it's simply smoke and mirrors for my clients and if I add and remove lawyers dynamically it obviously breaks.

Here's my HTML/PHP. Any ideas? In don't even know where to start...

<?php foreach ($team as $lawyer) { ?>
<div id="<?php echo $lawyer->id?>_button" class="lawyer_title_small title-<?php echo $lawyer->title_colour;?>">
    <h2><?php echo $lawyer->name?></h2>
</div>
<?php }?>

<?php foreach ($team as $lawyer) { ?>
<div id="<?php echo $lawyer->id;?>" class="team_info">
    <div class="team_info_left">
        <img src="img/team/<?php echo $lawyer->picture;?>"/>
        <table>
            <tr>
                <th style="color: #9e0039;">TEL:</th>
                <th><?php echo $lawyer->tel;?></th>
            </tr>
            <tr>
                <th style="color: #9e0039;">GSM:</th>
                <th><?php echo $lawyer->gsm;?></th>
            </tr>
            <tr>
                <th style="color: #9e0039;">MAIL:</th>
                <th style="font-size:15px;"><?php echo $lawyer->mail;?></th>
            </tr>
        </table>
    </div>
    <div class="team_info_right">
        <h4>OVER <?php echo $lawyer->name;?></h4>
        <?php echo $lawyer->about;?>
        <h4>VOORKEURKENNIS:</h4>
        <?php echo $lawyer->knowledge;?>
    </div>

</div>
<?php }?>

If it isn't clear, I'll try to explain further. Thanks a lot!

  • 写回答

1条回答 默认 最新

  • doushao1948 2014-09-19 00:17
    关注

    The more relevant code is the javascript in /varlaw/js/main.js, though it is also useful to have seen the PHP.

    First, in PHP, build the lawyer buttons inside a containing div as follows :

    <div><!-- unstyled structural div -->
        <?php foreach ($team as $lawyer) { ?>
        <div id="<?php echo $lawyer->id?>_button" class="lawyer_title_small title-<?php echo $lawyer->title_colour;?>">
            <h2><?php echo $lawyer->name?></h2>
        </div>
        <?php }?>
    </div>
    

    Second, in PHP build the "team_info" divs inside a containing div as follows :

    <div><!-- unstyled structural div -->
        <?php foreach ($team as $lawyer) { ?>
        <div id="<?php echo $lawyer->id;?>" class="team_info">
            <div class="team_info_left">
                <img src="img/team/<?php echo $lawyer->picture;?>"/>
                <table>
                    <tr>
                        <th style="color: #9e0039;">TEL:</th>
                        <th><?php echo $lawyer->tel;?></th>
                    </tr>
                    <tr>
                        <th style="color: #9e0039;">GSM:</th>
                        <th><?php echo $lawyer->gsm;?></th>
                    </tr>
                    <tr>
                        <th style="color: #9e0039;">MAIL:</th>
                        <th style="font-size:15px;"><?php echo $lawyer->mail;?></th>
                    </tr>
                </table>
            </div>
            <div class="team_info_right">
                <h4>OVER <?php echo $lawyer->name;?></h4>
                <?php echo $lawyer->about;?>
                <h4>VOORKEURKENNIS:</h4>
                <?php echo $lawyer->knowledge;?>
            </div>
    
        </div>
        <?php }?>
    </div>
    

    You now have two congruent structural divs; corresponding child elements of these divs have the same index within their parent div. This is important as it allows for very simple, efficient javaScript.

    Third, in javaScript replace all the $("#marc_button")..., $("#dirk_button")... etc. code with :

    $(".lawyer_title_small").on("click", function() {
        var $this = $(this);
        if ($this.hasClass("lawyer_title_main")) { //this trap may be unnecessary
            return;
        } else {
            //hide all lawyer divs, then selectively show the appropriate lawyer div
            //ie. the div with the same index as the button that was clicked
            $(".team_info").hide().eq($this.index()).show();
        }
    });
    

    If the trap is indeed unnecessary, then the click handler will simplify to :

    $(".lawyer_title_small").on("click", function() {
        $(".team_info").hide().eq($(this).index()).show();
    });
    

    Now, your PHP can build the page with as few or as many Lawyers as it likes and the javascript will handle them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么