您好,欢迎访问一九零五行业门户网

如何判断鼠标是否在DIV的区域内_javascript技巧

今天研究了一下这个问题,也普及了一下知识吧。
方法一:
通过mouseover,mouseout来触发事件,才判断鼠标是否在该区域。 但是这种方法的局限性就是,必须要触发mouseover,或mouseout,mouseleave事件才能知道。
复制代码 代码如下:
function chkin()
    {
  div_1.innertext = 现在你把鼠标移入层了!;
  div_1.style.font = normal black;
 }
   function chkout()
    {
  div_1.innertext = 现在你把鼠标移出层了!;
  div_1.style.font = bold red;
 }
复制代码 代码如下:
onmouseover=chkin() onmouseout=chkout()>this is a div
方法二:
复制代码 代码如下:
function   checkin(e){
var   x=window.event.clientx;
var   y=window.event.clienty;
var   str= ' ';
for(i=0;i             var   obj=document.body.children[i];
          if(x> obj.offsetleft
                                  &&x                           &&y> obj.offsettop
                        &&y                 str+= ' \n ';
          }else{
                str+= ' \n ';
        }
  }
alert(str);
}
document.onclick=checkin
方法三:这个方法是最简单的实用的。
复制代码 代码如下:
if(mydiv.contains(window.event.srcelement))
即 if(mydiv.contains(鼠标位置的元素对象))具体情况还是要根据自己需要来选择,我是调试了一下方法三,但是具体也没使用上。 其他方法,继续研究中。
其它类似信息

推荐信息