几个常用JavaScript函数

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

//清理不可见多于字符 
/**
 * 清理不可见多于字符
 * @param {string} string 事件类型
 * @return {string} string
 */
var trim = function(string){
    var string = string + "";
    return string.replace(new RegExp("(^[\\\\s\\\\t\\\\xa0\\\\u3000]+)|([\\\\u3000\\\\xa0\\\\s\\\\t]+\\x24)", "g"), "");
};

//布尔类型或者01之间的切换
/**
 * 布尔类型或者01之间的切换
 * @param {Boolean, 0|1} val 值
 * @return {Boolean, 0|1} 返回反
 */
var turn = function(val){
    return typeof val == "boolean" ? (1 - val) : !val;
};

//为目标元素添加事件监听器
/**
 * 为目标元素添加事件监听器
 * @param {HTMLElement|string|window} ele 目标元素或目标元素id
 * @param {string} type 事件类型
 * @param {Function} callback 需要添加的监听器
 */   
var on = function(ele, type, callback) {
    ele = typeof ele == "string" ? document.getElementById(ele) : ele;
    ele.attachEvent ? ele.attachEvent("on" + type,
        function (type) {
            callback.call(ele, type);
        }) : ele.addEventListener && ele.addEventListener(type, callback, false);
};

//按照Attribute属性获得元素
/**
 * 按照Attribute属性获得元素
 *
 * @param attr 属性名称
 * @param val 属性的值
 * @param parent 父节点,默认全部
 * @param tag TAG名称,默认所有
 * @return Elements 符合所有条件的DOM对象
 */
var walk = function(attr, callback, parent, tag) {
    var eles = (tag == "*" && document.all) ? document.all : (parent || document).getElementsByTagName(tag || "*");
    var ret = [];
    var val, item;
    for (var i = 0; i < eles.length; i++) {
        item = eles[i];
        val = item.getAttribute(attr);
        if (typeof val == "string" && val.length > 0) {
            callback && callback.call(null, item, val);
            ret.push(item);
        }
    }
    return ret;
};

//目标元素绝对于屏幕的坐标
/**
 * 为目标元素添加事件监获得元素绝对于屏幕的坐标
 * @param {element} ele 目标元素
 * @return {Integer, Integer} 返坐标对象
 */
var pos = function(ele) {
    var x = 0, y = 0;
    if (ele && ele.offsetParent) {
        do {
            x += ele.offsetLeft;
            y += ele.offsetTop;
        } while (ele = ele.offsetParent);
    }
    return { x:x, y:y };
}

//获取鼠标当前的坐标xy 
/**
 * 获取鼠标当前的坐标xy
 * @param {event} 全局事件对象
 * @return {Integer, Integer} xy坐标
 */
var mouse = function (event) {
    var e = event || window.event;
    if(e.type != "mouseover") return null;
    var x = 0, y = 0;
    if (e.pageX || e.pageY) {
        x = e.pageX;
        y = e.pageY;
    } else if (e.clientX || e.clientY) {
        x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    return {x:Math.abs(x), y:Math.abs(y)}
}

//无返回发送数据
/**
 * 请求统计行动,将数据通知给服务器
 * @param {Array|string|Object} query 数据字符串或者对象
 */
var log = function(url, query) {
    var ele = new Image(), data = [];
    window["$BAIDU:DATA$"] = ele;
    ele.onload = ele.onerror = ele.onabort = function () {
        ele.onload = ele.onerror = ele.onabort = null;
        ele = window["$BAIDU:DATA$"] = null;
    };
    for (var n in query) {
        data.push(n + "=" + encodeURIComponent(query[n]));
    }
    data.push("ot=" + new Date().getTime());
    query = data.join("&");
    ele.src = url + "?" + query;
    return true;
};