这是一个拖拽代码,问题如注释所示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>代码非常简洁且兼容多浏览器的拖动层</title>
</head>
<body>
<div id="f" style="position: absolute; width: 500px; height: 350px; background-color: #ccc; top: 150px; left: 200px; z-index: 101; border: solid 1px blue;">
<div id="title" style="background-color: Blue; cursor: move; height: 20px; color: #fff;font-size: 13px; padding-top: 5px; padding-left: 10px;">这里是层标题jb51.net</div>
</div>
<script type="text/javascript">
var posX;
var posY;
fdiv = document.getElementById("f");
document.getElementById("title").onmousedown=function(e)
{
var e=window.event||e
posX = e.clientX - parseInt(fdiv.style.left);
posY = e.clientY - parseInt(fdiv.style.top); document.addEventListener('mousemove',mousemove,false); //function(e){mousemove(e)}为什么不行
e.preventDefault() document.onmouseup = function()
{
document.removeEventListener('mousemove',mousemove,false) ;
}function mousemove(ev)
{
ev=window.event||ev
fdiv.style.left = (ev.clientX - posX) + "px";
fdiv.style.top = ev.clientY - posY + "px";
} }
</script>
</body>
</html>document.addEventListener('mousemove',mousemove,false)与
document.removeEventListener('mousemove',function(e){mousemove(e)},false) ;有什么不同,为什么第二种写法会使.onmouseup函数不起作用?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>代码非常简洁且兼容多浏览器的拖动层</title>
</head>
<body>
<div id="f" style="position: absolute; width: 500px; height: 350px; background-color: #ccc; top: 150px; left: 200px; z-index: 101; border: solid 1px blue;">
<div id="title" style="background-color: Blue; cursor: move; height: 20px; color: #fff;font-size: 13px; padding-top: 5px; padding-left: 10px;">这里是层标题jb51.net</div>
</div>
<script type="text/javascript">
var posX;
var posY;
fdiv = document.getElementById("f");
document.getElementById("title").onmousedown=function(e)
{
var e=window.event||e
posX = e.clientX - parseInt(fdiv.style.left);
posY = e.clientY - parseInt(fdiv.style.top); document.addEventListener('mousemove',mousemove,false); //function(e){mousemove(e)}为什么不行
e.preventDefault() document.onmouseup = function()
{
document.removeEventListener('mousemove',mousemove,false) ;
}function mousemove(ev)
{
ev=window.event||ev
fdiv.style.left = (ev.clientX - posX) + "px";
fdiv.style.top = ev.clientY - posY + "px";
} }
</script>
</body>
</html>document.addEventListener('mousemove',mousemove,false)与
document.removeEventListener('mousemove',function(e){mousemove(e)},false) ;有什么不同,为什么第二种写法会使.onmouseup函数不起作用?
document.addEventListener('mousemove',function(e){mousemove(e)},false)
{
ev=window.event||ev
fdiv.style.left = (ev.clientX - posX) + "px";
fdiv.style.top = ev.clientY - posY + "px";
} } function(e){mousemove(e)}这两个函数虽然是干同一件事的 但本质上他们不是一回事