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

时间:2013-08-24 04:48:22

标签: javascript html

如果在第一行中单击了另一个按钮,如何在第二行和第三行中隐藏所有按钮,如果在第二行中单击另一个按钮,它将隐藏该列的按钮并显示该列的按钮点击了吗?我正在尝试为我的网站获得类似于http://gazelle.com/的表单,但这对我来说有点困难。

示例:如果我点击,“iPhone” - 按钮iphone 5,iphone 4s,iphone 4和iphone 3gs显示,然后点击,“iphone 5” - 按钮at& t,sprint,verizon,unlocked,and在第一行下的其他节目,然后我改变主意,决定点击“平板电脑”在第一个,“平板电脑”的按钮显示,但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/

3 个答案:

答案 0 :(得分:1)

试试这个我希望它能解决您的要求。 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>

答案 1 :(得分:0)

试试这个:

<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>

答案 2 :(得分:0)

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

你需要jQuery才能工作,所以jQuery需要在这个脚本之前加载,无论你决定使用哪个选项。

将此标记放在标记中的脚本标记中。或者在单独的javascript文档中准备好文档:

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

让我获得更优雅顺畅的解决方案