试图改变javascript中的文本位置

时间:2013-10-14 13:34:14

标签: javascript

我在html中制作了四个按钮(输入类型=“按钮”),并希望它们在点击时运行功能但没有发生任何事情。 html代码是:

<div id="newtext" style="position:absolute; top:100px;">
<p>this is Text</p>
</div>
<form>
<input type="button" name="moveLayer" value="move Left" onclick="move('left');">
<input type="button" name="moveLayer" value="move Right" onclick="move('right');">
<input type="button" name="moveLayer" value="move Up" onclick="move('up'); ">
<input type="button" name="moveLayer" value="move Down" onclick="move('down'); ">
</form>

,脚本是:

function move(direction) {
var layerText = document.getElementByID("newtext");

switch(direction) {

case "left":
    layerText.style.left=50;
    break;

case "right":
    layerText.style.right=150;
    break;

case "up":
    layerText.style.up=50;
    break;
case "down":
    layerText.style.down=150;
    break;
default:
it is not working;
}

}

提前感谢..

2 个答案:

答案 0 :(得分:0)

layerText.style.up = 50;                  ^

layerText.style.down = 150;                  ^ 没有财产上下。改为使用顶部和底部;

默认:

it is not working;

可能导致语法错误

答案 1 :(得分:0)

这应该有用(没有style.up和style.down这样的东西):

function move(direction) {
    var layerText = document.getElementById("newtext");

    switch (direction) {

        case "left":
            layerText.style.left = "50px";
            break;

        case "right":
            layerText.style.left = "150px";
            break;

        case "up":
            layerText.style.top = "50px";
            break;

        case "down":
            layerText.style.top = "150px";
            break;
    }
}

演示:http://jsfiddle.net/GE3C5/2/