繁体   English   中英

单击另一个DIV时如何隐藏多个显示/隐藏DIV

[英]How to hide multiple Show/Hide DIV's when another DIV is clicked

如果在第一行中单击了其他按钮,并且在第二行中单击了另一个按钮,则如何隐藏第二行和第三行中的所有按钮,它将隐藏该列的按钮并显示被单击的列的按钮? 我正在尝试为我的网站实现类似于http://gazelle.com/的形式,但对我来说却有点困难。

示例:如果我单击“ iPhone”-显示iphone 5,iphone 4s,iphone 4和iphone 3gs按钮,然后单击“ iphone 5”-at&t,sprint,verizon,已解锁以及其他显示按钮行,然后我改变主意,决定先单击“平板电脑”,“平板电脑”的按钮会显示出来,但iPhone 5的按钮会回到隐藏状态,但“平板电脑”的按钮会显示出来。

<!DOCTYPE html>
<html>
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>

    <script type="text/javascript">
        function showHide(obj) {
            var div = document.getElementById(obj);
            if (div.style.display == 'none') {
                div.style.display = '';
            }
            else {
                div.style.display = 'none';
            }
        }
    </script>

</head>
<body>

    <table>
        <td>
        <a href="#" onclick="showHide('hidden_div'); return false;"><button>iPhone</button></a>
        <a href="#" onclick="showHide('q2'); return false;"><button>iPod</button></a>
        <a href="#" onclick="showHide('q3'); return false;"><button>Tablet</button></a>
        </td>
    </table>

    <div id="hidden_div" style="display: none;">
        <button onclick="showHide('q4'); return false;">iPhone 5</button>
        <button>iPhone 4S</button>
        <button>iPhone 4</button>
        <button>iPhone 3GS</button>
    </div>

    <div id="q2" style="display: none;">
        <button>iPod Touch</button>
        <button>iPod Nano</button>
        <button>Classic iPod</button>
    </div>

    <div id="q3" style="display: none;">
        <button>iPad</button>
        <button>Windows</button>
        <button>Nook</button>
    </div>

    <div id="q4" style="display: none;">
        <button onclick="showHide('q'); return false;">AT&T</button>
        <button>Sprint</button>
        <button>Verizon</button>
        <button>Unlocked</button>
        <button>Other</button>
    </div>

</body>
</html>

http://jsfiddle.net/EbbCc/

试试这个,我希望它能解决您的要求。 http://jsfiddle.net/satheeaseelan/Qf5B3/

<!DOCTYPE html>
<html xmlns=" http://www.w3.org/1999/xhtml">
  <head>
    <style type="text/css">.categories, .serviceProviders button{ display:none;}</style>
  </head>
  <body>
    <table>
      <td>
        <a href="#" class="mainHead"><button>iPhone</button></a>
        <a href="#" class="mainHead"><button>iPod</button></a>
        <a href="#"  class="mainHead"><button>Tablet</button></a>
      </td>
   </table>
        <div class="categories">
    <button class="iphone5">iPhone 5</button>
    <button class="iphone4s">iPhone 4S</button>
    <button class="iphone4">iPhone 4</button>
    <button class="iphone3gs">iPhone 3GS</button>
    </div>

        <div class="categories">
    <button class="ipodT">iPod Touch</button>
    <button  class="ipodN">iPod Nano</button>
    <button  class="ipodC">Classic iPod</button>
    </div>

    <div class="categories">
    <button class="ipad">iPad</button>
    <button class="windows">Windows</button>
    <button class="nook">Nook</button>
    </div>

    <div class="serviceProviders">
    <button class="iphone5 ipodN iphone4s ipad">AT &amp; T</button>
    <button class="nook ipodN iphone3gs ipad">Sprint</button>
    <button class="iphone5 iphone4s windows">Verizon</button>
    <button class="nook ipodN ipodT ipad iphone4">Unlocked</button>
    <button class="iphone5 iphone3gs windows">Other</button>
    </div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(e) {
            $('.mainHead').each(function(index, element) {
                $(this).click(function(){
                    $('.categories').hide();
                    $('.serviceProviders button').hide();
                    $('.categories').eq(index).show();      
                });
            });

            $('.categories button').each(function(index, element) {
                 $(this).click(function(){
                    var temp = $(this).attr('class');
                    $('.serviceProviders button').hide();
                    $('.serviceProviders button').each(function(){
                        if($(this).hasClass(temp)){
                            $(this).show();
                        }
                    })
                 });
            });

        });
    </script>

    </body>
    </html>

尝试这个:

<table>
<td>
<a href="#" onclick="showHide(1);"><button>iPhone</button></a>
<a href="#" onclick="showHide(2);"><button>iPod</button></a>
<a href="#" onclick="showHide(3)"><button>Tablet</button></a>
</td>
</table>
<div id="q1" style="display: none;">
    <button onclick="subShowHide('1');">iPhone 5</button>
    <button>iPhone 4S</button>
    <button>iPhone 4</button>
    <button>iPhone 3GS</button>
</div>
<div id="q2" style="display: none;">
    <button>iPod Touch</button>
    <button>iPod Nano</button>
    <button>Classic iPod</button>
</div>
<div id="q3" style="display: none;">
    <button>iPad</button>
    <button>Windows</button>
    <button>Nook</button>
</div>
<div id="qq1" style="display: none;">
    <button onclick="subSubShowHide('1');">AT&T</button>
    <button>Sprint</button>
    <button>Verizon</button>
    <button>Unlocked</button>
    <button>Other</button>
</div>
<div id="qqq1" style="display: none;">
    <button>test1</button>
    <button>test2</button>
    <button>test3</button>
</div>
<script>
function showHide(obj) {
    for(i=1;i<=1;i++){
        document.getElementById('qq'+i).style.display = 'none';
    }
    for(i=1;i<=1;i++){
        document.getElementById('qqq'+i).style.display = 'none';
    }
    for(i=1;i<=4;i++){
        if (i == obj) {
            document.getElementById('q'+i).style.display = 'block';
        } else {
            document.getElementById('q'+i).style.display = 'none';
        }
    }
    return false;
}
function subShowHide(obj){
    for(i=1;i<=1;i++){
        document.getElementById('qqq'+i).style.display = 'none';
    }
    for(i=1;i<=1;i++){
        if (i == obj) {
            document.getElementById('qq'+i).style.display = 'block';
        } else {
            document.getElementById('qq'+i).style.display = 'none';
        }
    }
    return false;
}
function subSubShowHide(obj){
    for(i=1;i<=1;i++){
        if (i == obj) {
            document.getElementById('qqq'+i).style.display = 'block';
        } else {
            document.getElementById('qqq'+i).style.display = 'none';
        }
    }
    return false;
}
</script>

这是很糟糕的做法,但这是解决方案:

您需要jQuery才能正常工作,因此无论您决定使用哪个选项,都需要在此脚本之前加载jQuery。

将其放在脚本标签中。 或在准备好文档的单独javascript文档中:

function showHide(obj) {
  $('#' + obj).toggle('fast');
  console.log('#' + obj);
}

为我提供更优雅,更流畅的解决方案

暂无
暂无

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

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