使用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";
}