dongyi6269 2015-06-09 09:07
浏览 123
已采纳

文本显示和隐藏按钮php / js

How can I apply the multiple texts? With a text it works:

<dl>
    <dt>
        <img class="empfehlung-bild" style="max-width: 300px; height: auto; float: left; margin-right: 15px; margin-bottom: 15px;" src="<?php echo $bild; ?>" />
        <h1><?php echo $title; ?></h1>
        <p>
            <?php echo $text; ?>
        </p><br/>
        <button id="<?php echo $i; ?>" style="float:right;">Details</button><br/><br/>
    </dt>
    <dd style="float:left;">
        <h2><?php echo $secret_title; ?></h2>
        <button id="<?php echo $i; ?>" style="float:right;">X</button>
        <p>
            <?php echo $secret_text; ?>
        </p>
    </dd>
</dl>

The script:

<script type="text/javascript">
    $(document).ready(function() {
        $("button").click(function() {
            $("dd").slideToggle("slow");
        });
    });
</script>

dd style is display: none. $i is a counter (while)

The problem is, if I have more than one dl, it closes and opens all of them.

  • 写回答

1条回答 默认 最新

  • douxian7808 2015-06-09 09:14
    关注

    This will select and toggle all instances of dd on the entire page.

    $("button").click(function () {
      $("dd").slideToggle("slow");
    });
    

    This will select and toggle the intended instance of dd, closest to the button you pressed.

    $("button").click(function () {
      $(this).closest('dl').find('dd').slideToggle("slow");
    });
    

    It takes the button you actually pressed with ($this), finds the parent dl with .closest('dl') and then looks for the dd inside that instance of dl with .find('dd').

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

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备