[整理]怎么判断鼠标左键按住不放
整理自:http://topic.csdn.net/u/20091125/14/f7fd42d7-fb20-4acc-9623-3f5640c19411.html
比如:按住鼠标左键进行文本选定操作;鼠标写字等。
Click_Me 提供以下代码:
<!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> <meta http-equiv="Content-Type" content="textml; charset=utf-8" /> <title>无标题文档</title> </head> <body> <div id="odiv" style="width:100px; height:100px; border:1px solid red">按住鼠标移上来</div> <script type="text/javascript"> <!-- var isBound = false; document.onmousedown = function(){ isBound = true; } document.onmouseup = function(){ isBound = false; } document.getElementById('odiv').onmouseover = function(){ if(!!isBound) this.style.backgroundColor = 'blue'; } //--> </script> </body> </html>
其实,给控件绑定onmouseover事件,如果页面上多个控件需要此功能的话,就很麻烦。可以设置document.onmouseover事件,并通过window.event.srcElement(firefox下是event.target)当前事件的元素,来给当前控件提供功能。