欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jquery 鼠标点击元素外面发生的事件 有大用

鼠标点击区域之外之内产生的事件


可以试试blur事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunhuwh/article/details/41246145

  1. <script type="text/javascript">
  2. window.onload = function() {
  3. document.onclick = function(e) {
  4. var ele = e ? e.target : window.event.srcElement;
  5. if(ele.id !== 'the_div') {
  6. document.getElementById('the_div').style.display = 'none';
  7. // 以下为测试代码,可删除
  8. alert('一秒后 div 恢复');
  9. setTimeout(function() {
  10. document.getElementById('the_div').style.display = 'block';
  11. }, 1000);
  12. // 结束测试代码
  13. }
  14. };
  15. };
  16. </script>
  17. <div id="the_div" style="width: 100px; height: 100px; background: #f00;"></div>


来自 https://blog.csdn.net/sunhuwh/article/details/41246145



jQuery鼠标点击其他元素是什么事件?

我要单击一个元素的时候让它显示一个div,然后单击其他地方的时候再隐藏这个div,这是什么事件,还是去要判断一下是哪个元素?


可以试试blur事件


3个回答
匿名用户 
2018-06-25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>  
<html>  
   
    <head>  
        <meta charset="UTF-8">  
        <script type="text/javascript" src="js/jquery.min.js"></script>  
        <title></title>  
        <script type="text/javascript">  
            $(document).bind('click', function(e) {  
                var e = e || window.event; //浏览器兼容性   
                var elem = e.target || e.srcElement;  
                while (elem) { //循环判断至跟节点,防止点击的是div子元素   
                    if (elem.id && elem.id == 'test') {  
                        return;  
                    }  
                    elem = elem.parentNode;  
                }  
                $('#test').css('display', 'none'); //点击的不是div或其子元素   
            });  
        </script>  
    </head>  
   
    <body>  
        <div id="test" style="width: 300px; height: 300px; background-color: #CBC7BC;">  
            div测试内容,点击其它地方会隐藏此div模块。  
        </div>  
    </body>  
   
</html>

来自 https://zhidao.baidu.com/question/2272379694233530868.html

普通分类: