2

这里的目标是点击 1.gif,所有带有 .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>
4

3 回答 3

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';
    }
}

If you were using a JavaScript library, like jQuery, this would be much easier to do. Also, as has been mentioned, you need quotes around your arguments to panelTransition.

<img class="panel1" src=1.gif onclick="panelTransition('panel1', 'panel2')" />
<img class="panel2" src=2.gif />
于 2012-05-18T19:34:44.443 回答
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 图像都会消失,您将无法再次单击它们。

于 2012-05-18T19:30:10.083 回答
0
<img class="panel1" src=1.gif onclick="panelTransition('panel1','panel2')" />

我认为你需要那里的报价

于 2012-05-18T19:30:48.743 回答