【问题标题】:show/hide div is opening all divs when button is clicked instead of just one显示/隐藏 div 在单击按钮时打开所有 div 而不是一个
【发布时间】:2013-04-21 08:09:59
【问题描述】:

我正在尝试在带有按钮的 div 上使用显示/隐藏,但是当单击按钮时它会立即打开所有 div 这是我在http://starsQA.com/jen-lilley-interviews上尝试的页面

代码如下:

<?php
    include("db_conn.php");
    $qry_string = "select * from questions WHERE starID = 8 && returned = 1 GROUP BY `reason` ORDER BY `edited` DESC, starID ASC, `reason`";
    $prep = $pdo_conn->prepare($qry_string);
    $prep->execute(array($starid));
    while ($row = $prep->fetch(PDO::FETCH_ASSOC)) {
        echo "<button class='show_hide' id='tglbtn{$row['reason']}'>{$row['reason']}</button>";
//        echo "<div style='clear:both;'></div>";
        echo "<div style='color:black;' class='slidingDiv' id='tgldiv{$row['reason']}'><b><u>{$row['reason']}</u></b><br><ol>";
        $qry_stringq = "select * from questions WHERE starID = 8 && returned = 1 && reason = '{$row['reason']}' ORDER BY starID ASC, `reason`";
        $prepq = $pdo_conn->prepare($qry_stringq);
        $prepq->execute(array($starid));
            echo "<ol style='margin:30px'>";
            while ($rowq = $prepq->fetch(PDO::FETCH_ASSOC)) {
                echo "<li><b>{$rowq['question']} - {$rowq['whoAsked']}</b><br>
                        {$rowq['response']}</li><br>";

            }
            echo "<ol'></div>";
        echo "<div style='clear:both;'></div>";
     }
     echo "<br><br>";
    ?>

javascript:

<script type="text/javascript">

$(document).ready(function(){

        $(".slidingDiv").hide();
        $(".show_hide").show();

    $('.show_hide').click(function(){
    $(".slidingDiv").slideToggle();


                    $no = $(this).attr('id').substring(6);

                    if($("#tgldiv" + $no).css("display") == "inline-block") {
                        $("#tgldiv" + $no).css("display", "none");
                        $(this).html($namearray[$no]);
                    }
                    else {
                        $("#tgldiv" + $no).css("display", "inline-block");
                        $namearray[$no] = $(this).html();
                        $(this).html("hide");
                    }
                 });   
});

</script>

css:

            .slidingDiv {
    height:300px;
    background-color: #99CCFF;
    padding:20px;
    margin-top:10px;
    border-bottom:5px solid #3399FF;
}

.show_hide {
    display:none;
}

有人对如何解决此问题有任何想法吗??? 已修复

在页面加载/打开时如何阻止 div 打开? 仍然需要修复

在每个 div 中修复内容也会很有帮助

【问题讨论】:

  • 你的javascript中有2个$(".show_hide").click(function() {点击函数。
  • 删除了,没有改变(也从上面的代码中删除了)
  • 您删除了哪个点击功能?
  • 我删除了第二个

标签: php javascript html pdo show-hide


【解决方案1】:
$('.show_hide').click(function(){
$("this").next('.slidingDiv').slideToggle(); // need target one .slidingDiv  
});

【讨论】:

  • 那个按钮不要点击
  • 按钮仍未点击
【解决方案2】:

那是因为$(".slidingDiv") 引用了类为slidingDiv 的所有元素。你需要的是上下文。您需要离您的按钮最近的元素。看起来您所有的扩展 div 都是按钮的邻居,因此您可以为此使用 jQuerys siblingsnext

$('.show_hide').click(function(){
   $(this).next('.slidingDiv').slideToggle();
});

另外,请不要在您的 id 属性中添加空格,这会破坏您的页面。

【讨论】:

  • 哎呀,太着急了,马上试试
  • 现在打开一个,你知道如何在页面加载/打开时停止 div 的打开吗?
  • 交换这个:echo "&lt;div style='color:black;' class='slidingDiv' id='tgldiv{$row['reason']}'&gt;&lt;b&gt;&lt;u&gt;{$row['reason']}&lt;/u&gt;&lt;/b&gt;&lt;br&gt;&lt;ol&gt;"; 和这个echo "&lt;div style="color: black; display: none;" class='slidingDiv' id='tgldiv{$row['reason']}'&gt;&lt;b&gt;&lt;u&gt;{$row['reason']}&lt;/u&gt;&lt;/b&gt;&lt;br&gt;&lt;ol&gt;";
  • 谢谢,我进去后试试,你知道如何让内容适合每个 div 吗?
  • 在页面顶部的标题中,您有&lt;style&gt; 标签。在第 20 行,您设置了 height:300px;。将其更改为 height:auto 并且 div 的高度将适应:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 2016-05-21
  • 2017-09-22
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2020-02-23
相关资源
最近更新 更多