zoukankan      html  css  js  c++  java
  • jquery实现点击空白的事件

    //点击空白扩展
    jQuery.fn.extend({
        clickOther: 
    function() {
            
    if (window.click_other == null) {
                window.click_other 
    = new Array();
            }
            window.click_other.push({ target: 
    this, func: arguments[0] });
            $(document.body).click(
    function(e) {
                
    for (var i = 0; i < click_other.length; i++) {
                    
    var item = window.click_other[i];
                    
    var target = item.target;
                    
    if (typeof (target) == "String") target = $(target)[0];

                    
    if (e.target == target) {
                        
    continue;
                    }
                    
    var index = $(e.target).parents().index(target);
                    
    if (index == -1) {
                        item.func();
                    }
                }
            });

        },
        unClickOther: 
    function() {
            
    if (window.click_other == null) {
                
    return;
            }
            
    for (var i = 0; i < window.click_other.length; i++) {
                
    var item = window.click_other[i];
                
    if (item.target == this) {
                    window.click_other.splice(i, 
    1);
                }
            }
        }
    }); 
    //点击空白扩展
    jQuery.fn.extend({
        clickOther: 
    function() {
            
    function isInside(e, target) {
                
    var x = e.pageX;
                
    var y = e.pageY;
                
    var left = target.offset().left;
                
    var width = target.width();
                
    var top = target.offset().top;
                
    var height = target.height();

                
    if (x > left && x < left + width && y > top && y < top + height) return true;
                
    return false;
            }
            
    if (window.click_other == null) {
                window.click_other 
    = new Array();
            }
            
    this.except = null;
            
    var func = arguments[0];
            
    if (typeof (func) == "string") {
                
    this.except = document.getElementById(func);
                func 
    = arguments[1];
            }
            window.click_other.push({ target: 
    this, func: func });
            $(document).click(
    function(e) {
                
    for (var i = 0; i < click_other.length; i++) {
                    
    var item = window.click_other[i];
                    
    var target = $(item.target);
                    
    if (isInside(e, target)) continue;
                    
    if (item.target.except) {
                        
    var except = $(item.target.except);
                        
    if (isInside(e, except)) continue;
                    }
                    item.func();
                }
            });

        },
        unclickOther: 
    function() {
            
    if (window.click_other == null) {
                
    return;
            }
            
    for (var i = 0; i < window.click_other.length; i++) {
                
    var item = window.click_other[i];
                
    if (item.target == this) {
                    window.click_other.splice(i, 
    1);
                    
    break;
                }
            }
        }
    }); 


  • 相关阅读:
    这 100 道 Python 题,拿去刷!!!
    快速入门 TensorFlow2 模型部署
    零基础入门 Kubernetes,你需要知道这些
    Java 面试必考难点,这一个教程全搞定
    Flask实战:如何从零开发“知乎”
    关于layui表格渲染templet解析单元格的问题
    清除input输入框的历史记录
    phpmyadmin导入导出大数据文件的办法
    yiiaseInvalidCallException The cookie collection is read only.
    Yii2.0关闭自带的debug功能
  • 原文地址:https://www.cnblogs.com/gateluck/p/1869092.html
Copyright © 2011-2022 走看看