在Javascript中移动对象

时间:2014-04-30 17:41:43

标签: javascript

我刚开始学习JS,对C / C ++有很好的html / xml知识,并且很想学习JS。

我跟着一本关于它的书,我正在写一个关于移动物体的章节,但是当我编写代码时,它并没有像它想象的那样移动物体。 这是我的代码:

<html>
<head>
<title> Moving elements </title>
<script type = "text/javascript">
function moveIt(movee, newTop, newLeft){
    dom = document.getElementById(movee).style;
    dom.top = newTop + "px";
    dom.left = newLeft + "px";
 }
</script>

</head>
<body>
<form action = "">
 <p>
    x coordinate: <input type = "text" id = "leftCoord" size = "3" />
    <br />
    y coordinate: <input type = "text" id = "topCoord" size = "3" />
    <br />
    <input type = "button" value = "Move it"
            on click =
                "moveIt('Nebula',
                document.getElementById('topCoord').value,
                document.getElementById('leftCoord').value)" />
    </p>
    </form>

    <div id = "Nebula" style = "position: absolute;
        top: 115px; left: 0;" />
        <img src = "nebula.jpg"
        alt = "(Picture of a nebula)" />
    </div>
</body>
</html>

因此表格和输入区域/按钮都在正确的位置,但是当我点击按钮时,图片不会移动,我只是无法弄清楚为什么......我&#39 ; m在w7上运行它。

1 个答案:

答案 0 :(得分:3)

请勿将其分开:on click,将其设为:onclick

工作: http://plnkr.co/edit/A7ddt6rtA8qf1ikBZJh1?p=preview