将CSS .class作为参数传递给javascript函数

时间:2012-05-18 19:27:07

标签: javascript html css

这里的目标是1.gif的onclick,带有.panel1类的所有东西都消失了(style.display.none),而.panel2类的所有内容都变得可见(style.display.inline)

我是新手。所以我认为这只是''或者“”

的语法问题
<!DOCTYPE html>

<html>

<head>
    <title>main</title>
    <meta charset="utf-8">

    <style type="text/css">
    .panel1 {display:inline;}
    .panel2 {display:none;}
    </style>

    <script type="text/javascript">
    function panelTransition(panelOut,panelIn)
        {
        document.getElementByClass(panelIn).style.display="inline";
        document.getElementByClass(panelOut).style.display="none";
        }
    </script>
</head>

<body>
<img class="panel1" src=1.gif onclick="panelTransition(panel1,panel2)" />
<img class="panel2" src=2.gif />
</body>

</html>

3 个答案:

答案 0 :(得分:4)

没有getElementByClass。它是getElementsByClassName,它返回一个项目数组,因此您需要修改代码以循环遍历它们。

function panelTransition(panelOut, panelIn) {
    var inPanels = document.getElementsByClassName(panelIn);
    for (var i = 0; i < inPanels.length; i++) {
        inPanels[i].style.display = 'inline';
    }

    var outPanels = document.getElementsByClassName(panelOut);
    for (var i = 0; i < outPanels.length; i++) {
        outPanels[i].style.display = 'none';
    }
}

如果您使用的是JavaScript库,比如jQuery,那么这将更容易实现。另外,如上所述,您需要引用panelTransition的参数。

<img class="panel1" src=1.gif onclick="panelTransition('panel1', 'panel2')" />
<img class="panel2" src=2.gif />

答案 1 :(得分:0)

<html>

<head>
    <title>main</title>
    <meta charset="utf-8">

    <style type="text/css">
    .panel1 {display:inline;}
    .panel2 {display:none;}
    </style>

    <script type="text/javascript">
    function panelTransition(panelOut,panelIn)
                {
                    // panelIn gets turned on
                    setDisplay(panelIn,"inline");

                    // panelOut gets turned off
                    setDisplay(panelOut,"none");
                }

                function setDisplay(className,displayState)
                {
                    // retrieve a list of all the matching elements
                    var list = document.getElementsByClassName(className);

                    // step through the list
                    for(i=0; i<list.length; i++) {
                        // for each element, set the display property
                        list[i].style.display = displayState;
                    }                        
                }
    </script>
</head>

<body>
<img class="panel1" src="1.gif" onclick="panelTransition('panel1','panel2')" />
<img class="panel2" src="2.gif" onclick="panelTransition('panel2','panel1')" />
</body>

</html>

或者你可以在jQuery中完成同样的事情

// fires when the page is up and running
$(document).ready(function(){

     // find all the panel1 elements, 
     // attach an on click handler
     $(".panel1").bind("click", function(){ 

            // find all the panel1 elements
            // set their css display property to inline
            $(".panel1").css("display","inline"); 

            // find all the panel2 elements
            // set their css display property to none
            $(".panel2").css("display","none");
     });

     $(".panel2").bind("click", function(){ 
            $(".panel2").css("display","inline"); 
            $(".panel1").css("display","none");
     });
});

您可以在此处了解有关jQuery的所有信息:http://www.jquery.com/

您只能运行一次代码,只要您单击panel1图像,所有panel2图像都将消失,您将无法再次点击它们。

答案 2 :(得分:0)

<img class="panel1" src=1.gif onclick="panelTransition('panel1','panel2')" />

我认为你需要引用