<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">
<title>随意拖动图片</title>
</head>
<bodyonLoad="remove()">
<div id="div1"onMouseOver="dragimages=div1;drag=1;"style="height:77px;left:10px;position:absolute;top:10px;width:90px">
<imgname="img1" alt="" border="0"src="/images/logo.gif" style="cursor:pointer ">
</div>
<p>
<scriptlanguage="javascript">
drag = 0;//没有拖动
move = 0;//没有移动
function mousedown()
{
if(drag)//鼠标划过去的时候赋值为1
{
X1 = window.event.x - parseInt(dragimages.style.left);//差距
Y1 = window.event.y - parseInt(dragimages.style.top);
dragimages.style.Index += 1;
move = 1;//让他移动
}
}
function mouseStop()
{
window.event.returnValue = false;
}
function mousemove()
{
if (move)
{
dragimages.style.left = window.event.x - X1;//鼠标位置-差距
dragimages.style.top = window.event.y - Y1;
}
}
function mouseup()
{
move = 0;//停止移动
}
function remove()
{
document.all.div1.onmousemove = mousemove;
document.all.div1.onmousedown = mousedown;
document.all.div1.onmouseup = mouseup;
document.all.div1.ondragstart = mouseStop;
}
</script>
</body>
</html>
关于event.returnvalue=false在ie中表示当做了某个判断,就不允许此事件继续执行。
<scriptlanguage="javascript" type="text/javascript">
function test()
{
if(event.keyCode == 46)
{
event.returnValue = false;
}
}
</script>
<inputtype="text" οnkeypress="test()"/>
在火狐中是:event.preventDefault();
<scriptlanguage="javascript" type="text/javascript">
function test(event)
{
if(event.charCode == 46)
{
event.preventDefault();
}
}
</script>
<inputtype="text" οnkeypress="test(event)"/>
此外window.event.returnValue=false放到提交表单中的onclick事件中则不会提交表单,如果放到超链接中则不执行超链接