在我看来,所有关于 html 拖放的教程都将 onmousedown 和 onmouseup 事件设置为文档而不是元素本身,并且正如我用下面的代码测试的那样,当单击元素时,onmousedown 和 onmouseup 事件都会被触发,但是当尝试将元素拖放到另一个地方时,不会触发 onmouseup 事件,
我使用下面的代码,我的问题是:为什么拖动时不会触发 onmouseup?我使用 Firefox 8.0.1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Border</title>
<style type="text/css">
body{
background-color: gray;
}
#page{
margin: 20px auto;
width:800px;
height:639px;
/*background-image: url("./img/board.jpg");*/
background-image: url("https://www.google.com/logos/2012/dickens-2012-HP.jpg");
background-repeat: no-repeat;
}
#target{
position: relative;
top: 10px;
left: 20px;
/*background-image: url("./img/target.jpg");*/
background-image: url("https://www.google.com/images/icons/product/chrome-48.png");
background-repeat: no-repeat;
width: 145px;
height:117px;
}
</style>
<script type="text/javascript">
function mylog(msg)
{
if(window.console && console.log){
console.log(msg);
}
}
window.onload = function(){
initdragdrop();
}
/*
todo: learn how to make a getObject()
function getObject(id){
var t = document.getElementById(id);
this.x = t.left;
this.y = t.top;
}
*/
function initdragdrop(){
var t = document.getElementById('target');
t.onmousedown = function(){
this.status = 'down';
mylog(this.status);
}
t.onmouseup = function(){
this.status = 'up';
mylog(this.status);
}
}
</script>
</head>
<body>
<div id="page">
<div id="target">
</div>
</div>
</body>
</html>