Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript移动图像_Javascript_Html - Fatal编程技术网

使用javascript移动图像

使用javascript移动图像,javascript,html,Javascript,Html,这个函数有什么问题 function moveColor() { document.getElementById(purple).style.marginRight = "34px"; } 使用此html: <div><img src="images/purple.png" id="purple" onclick="colorpurple()" onmouseover="moveColor()" style="cursor:pointer;"/></div&

这个函数有什么问题

function moveColor()
 {
 document.getElementById(purple).style.marginRight = "34px";
 }
使用此html:

<div><img src="images/purple.png" id="purple" onclick="colorpurple()" onmouseover="moveColor()" style="cursor:pointer;"/></div>


我还想让它在1秒内移动,但似乎无法解决这个简单的问题。

您需要将id放在引号中(以便将其视为字符串)


当前用法意味着
purple
引用一个未定义的变量,因此它有一个
未定义的
值,因此
文档.getElementById
方法不返回任何内容。

您似乎错过了函数getElementById上的引号

像这样:

function moveColor() { 
      document.getElementById('purple').style.marginRight = "34px";
}

您可能需要查看jQuery <代码>动画函数(页面中间有示例代码),“希望在1秒的时间内移动”是什么意思?你想让它滑过去吗?我还建议使用jQuery,您所要做的就是:
$(“#紫色”).animate({'margin-right':'34px'},1000)
function moveColor() { 
      document.getElementById('purple').style.marginRight = "34px";
}