在Chrome和Safari中使用JS引发麻烦

时间:2014-08-23 07:30:13

标签: javascript html

我已经就此示例here寻求帮助,但遇到了另一个问题。 此代码仅在我的Firefox浏览器中正常工作,但在其他浏览器中则不行。请帮忙。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Task 3</title>
    <script type='text/javascript'>
    var speed = 4;
    function showImage (src)
    {
    var div = document.createElement ("div");
    with (div.style)
    {
        width = "300px";
        heigth = "300px";
        border = "2px solid black";
        textAlign = "center";
        overflow = "hidden";
    }
    document.body.appendChild(div);
    img = document.createElement ("img");
    img.src = src;
    img.width = 300;
    img.height = 300;
    div.appendChild (img);
    }
    function keyDown (key)
    {
    var k = 0;
    if (key == 61 ) k = speed;
    if (key == 173 ) k = -speed;
    if (k != 0 );
    {
        img.width = img.width + k;
        img.height = img.height + k;
        img.style.margin = ((300 - img.height) / 2).toString() + "px";
    }
    }
    </script>
</head>
<body onkeydown="keyDown (event.keyCode)" onload="showImage ('image.jpg')">
</body>
</html>

它在Firefox中的运作方式:

enter image description here

0 个答案:

没有答案