简体   繁体   English

当单击按钮时,显示/隐藏div将打开所有div,而不仅仅是一个

[英]show/hide div is opening all divs when button is clicked instead of just one

I'm trying to use the show/hide on a div with buttons but it's opening all divs at once when the button is clicked here's the page i'm trying it out on http://starsQA.com/jen-lilley-interviews 我正在尝试在具有按钮的div上使用show / hide,但是单击按钮时它会一次打开所有div,这是我在http://starsQA.com/jen-lilley-interviews上尝试的页面

here's the code: 这是代码:

<?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: 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: CSS:

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

.show_hide {
    display:none;
}

anyone got any ideas on how to fix this issue??? 任何人都对如何解决此问题有任何想法??? FIXED 固定

also how do it stop the div's opening up while the page is loading/opening? 还如何在加载/打开页面时停止div的打开? STILL NEEDS FIXING 仍然需要解决

getting content to fix in each div would be great help too 在每个div中获取要修复的内容也将有很大帮助

That's because $(".slidingDiv") is referencing all elements with the class of slidingDiv . 这是因为$(".slidingDiv")引用所有带有slidingDiv类的slidingDiv What you need is context. 您需要的是上下文。 You need the nearest element to your button. 您需要最接近按钮的元素。 It looks like all your expanding divs are neighbours of your buttons, so you could use jQuerys siblings or next for this: 看起来您所有扩展的div都是按钮的邻居,因此您可以为此使用jQuery 兄弟姐妹next 兄弟姐妹

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

Also, please don't put spaces into your id attributes, it will break your page. 另外,请不要在您的id属性中添加空格,否则会破坏您的页面。

$('.show_hide').click(function(){
$("this").next('.slidingDiv').slideToggle(); // need target one .slidingDiv  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM