使JS部分更简单

时间:2014-12-18 20:24:46

标签: javascript html switch-statement

所以,我有一个这样的网页。在div之间切换的三个按钮并没有太多的代码。但是如果我有15个div来切换,那么会有更多的代码。有没有办法让我的JS代码更简单?

<!DOCTYPE>
<html>
<head>
    <style type="text/css">
        .info {
            width: 400px;
            height: 580px;
            margin: 40px;

            float: right;
        }

    </style>
</head>

<body>

    <div class="info" id="swapper-first" style="display:block; border:2px dashed red; padding:25px;">
        <p style="margin:0; color:red;">
            Red div
        </p>
    </div>

    <div class="info" id="swapper-second" style="display:none; border:2px dotted blue; padding:25px;">
        <p style="margin:0; color:blue;">
            Blue div
        </p>
    </div>

    <div class="info" id="swapper-third" style="display:none; border:2px solid green; padding:25px;">
        <p style="margin:0; color:green;">
            Green div
        </p>
    </div>

    <p style="text-align:center; font-weight:bold;">
        <a href="javascript:mred('swapper-first','swapper-second', 'swapper-third')">Red</a>
        <a href="javascript:mblue('swapper-first','swapper-second', 'swapper-third')">Blue</a>
        <a href="javascript:mgreen('swapper-first','swapper-second', 'swapper-third')">Green</a>
    </p>

    <script type="text/javascript">
        function mred(div1,div2,div3) {
            d1 = document.getElementById(div1);
            d2 = document.getElementById(div2);
            d3 = document.getElementById(div3);

            d1.style.display = "block";
            d2.style.display = "none";
            d3.style.display = "none";
        }

        function mblue(div1,div2,div3) {
            d1 = document.getElementById(div1);
            d2 = document.getElementById(div2);
            d3 = document.getElementById(div3);

            d1.style.display = "none";
            d2.style.display = "block";
            d3.style.display = "none"
        }

        function mgreen(div1,div2,div3) {
            d1 = document.getElementById(div1);
            d2 = document.getElementById(div2);
            d3 = document.getElementById(div3);

            d1.style.display = "none";
            d2.style.display = "none";
            d3.style.display = "block"
        }
    </script>
</body>
</html>

4 个答案:

答案 0 :(得分:2)

在JQuery中,这将是一种可能性。

function swapDiv(id){
    $('.info').hide(); //hides everything
    $('#'+id).show(); //shows the div with the ID that was passed in
}

只需添加&#39; divs&#39;所有div的课程。通过传递int想要保持可见的div的ID来调用该函数。

swapDiv('swapper-first')

答案 1 :(得分:1)

未经测试,但应该有效:

HTML:

将数据属性添加到href中,并使用要显示的div:

    <a href="javascript:divs()" data-value="swapper-third">Green</a>

JS:

function divs() {
            var a = document.querySelectorAll('div.info');
            var b = this.getAttribute('value');


    for (var i = 0;i<a.length;i++) {
        a[i].style.display = 'none'; 
    }
            document.getElementById(b).style.display = 'block';
}

答案 2 :(得分:0)

使用数组:

function mred(divs) {
   for (var i=0; i < divs.length; i++) {
     var div = document.getElementById(divs[i]);
     div.style.display = "block";
   }
}

答案 3 :(得分:0)

你去吧

<!DOCTYPE>
<html>
<head>
    <style type="text/css">
        .info {
            width: 400px;
            height: 580px;
            margin: 40px;

            float: right;
        }

    </style>
</head>

<body>

    <div class="info" id="swapper-first" style="display:block; border:2px dashed red; padding:25px;">
        <p style="margin:0; color:red;">
            Red div
        </p>
    </div>

    <div class="info" id="swapper-second" style="display:none; border:2px dotted blue; padding:25px;">
        <p style="margin:0; color:blue;">
            Blue div
        </p>
    </div>

    <div class="info" id="swapper-third" style="display:none; border:2px solid green; padding:25px;">
        <p style="margin:0; color:green;">
            Green div
        </p>
    </div>

    <p style="text-align:center; font-weight:bold;">
        <a href="javascript:mcolor('swapper-first')">Red</a>
        <a href="javascript:mcolor('swapper-second')">Blue</a>
        <a href="javascript:mcolor('swapper-third')">Green</a>
    </p>

    <script type="text/javascript">
    function mcolor(_id) {
        elements = document.getElementsByClassName('info')
        for(var i = 0; i < elements.length; i++) {
            if(elements[i].id == _id) {
                elements[i].style.display = 'block';
            } else {
                elements[i].style.display = 'none';
            }
        }
    }
    </script>
</body>
</html>