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 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重