这一段代码直接放到网页上运行,div是无法拖动的,但是如果把#div的样式,内联在div标签中,就可以拖动了,这是为什么?(
js实现拖动div块#div{
width: 500px;
height: 300px;
border: 2px solid #ccc;
position: absolute;
}
var px,py;
var div = document.getElementById('div');
div.onmousedown = function(e) {
var e = e || window.event;
px = e.clientX - parseInt(div.style.left);
py = e.clientY - parseInt(div.style.top);
div.onmousemove = mousemove;
}
div.onmouseup = function(){
div.onmousemove = null;
}
function mousemove(e){
var e = e || window.event;
div.style.left = (e.clientX-px)+'px';
div.style.top = (e.clientY-py)+'px';
}