0

ID'defaultone'または'defaulttwo'の画像のいずれかを選択すると、ID'actualone'の画像がそれに変更されるようにしようとしています。私は非常に近いことを知っていますが、どこかに小さなエラーがあります。誰か助けてくれませんか?

        <script type="text/javascript">
        // Popup window code
        function newPopup(url) {
            popupWindow = window.open(url,'popUpWindow','height=450,width=600,left=10,top=10,resizable=no,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
        }
        function bigimg(x) {
            x.style.height="65px";
            x.style.width="85px";
            x.style.opacity="0.5";
        }
        function defaultimg(x) {
            x.style.height="60px";
            x.style.width="80px";
        }
        function teamback(x) {
            document.getElementById("x").src = document.getElementById("defaultone").src;
        }
        </script>

    </head>
        <body>
            Your Team <img id="defaultone" onmouseover="bigimg(this)" onclick="teamback(this)" onmouseout="defaultimg(this)" src="cowboys.gif"> vs <img id="defaulttwo" onmouseover="bigimg(this)" onclick="teamback(this)" onmouseout="defaultimg(this)" src="giants.gif"><img src="" id="actualone" style="width:85px; height:65px;"><br><br>
            <img src="colts.gif"> vs <img src="bears.gif">
        </body>
</html>
4

4 に答える 4

3

JavaScriptメソッドteamover()で、sending-elementをdocument.getElementById("x");で誤って参照しています。要素"x"が存在しません。

これに更新してみてください:

function teamback(x) {
    // update the "actualone" image's source to the sending-image's source
    document.getElementById("actualone").src = x.src;
}
于 2012-08-10T22:55:55.913 に答える
2

jQueryの使用(HTMLドキュメントの先頭にすでに含まれている):

<script type="text/javascript">
    $(document).ready(function(){
        // For both
        $('#defaultone, #defaulttwo').click(function(){
            $('#actualone').attr('src', $(this).attr('src'));
        });
    });
</script>

1つの関数を使用するように書き直されました。

于 2012-08-10T22:58:06.643 に答える
0

次のようになります

function teamback(x) {
    x.src = document.getElementById("defaultone").src; // To change the x element's src to defaultone's src
}

thisあなたはそれのIDではなく要素自体を使用しているので。

于 2012-08-10T23:00:44.277 に答える
0

xこの行にアクセスしようとしているようです。

document.getElementById("x").src =

そのはず:

x.src =

また、などのコードのonclick後に​​は、次のようにセミコロンを付ける必要があります。

onclick="teamback(this);"
于 2012-08-10T23:01:54.557 に答える