博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tool.js
阅读量:6839 次
发布时间:2019-06-26

本文共 6414 字,大约阅读时间需要 21 分钟。

常用JS工具集合,持续更新 ...

//事件处理(2012.2.15 / 2012/3/27)             var EventUtil = {
addHandler:function(ele,type,handler){ //绑定事件 var list = {a:["addEventListener","attachEvent"],r:["removeEventListener","datachEvent"]}; var name = arguments[3]===true ? list["r"]:list["a"]; if(ele[name[0]]){
ele[name[0]](type,handler,false) }else if(ele.attachEvent){
ele[name[1]]("on"+type,handler); }else{
var handler = arguments[3]===true ? handler : null; ele["on"+type] = handler; } }, removeHandler:function(ele,type,handler){
//删除绑定 this.addHandler(ele,type,handler,true); }, getEvent:function(event){ //获取事件对象 return event ? event:window.event; }, getTarget:function(event){ //获取正在处理发生事件的对象 return event.target || event.srcElement; }, stopPropagation:function(event){ //阻止冒泡 if(event.stopPropagation){
event.stopPropagation(); }else{
event.cancelBubble = true; } }, preventDefalut:function(event){ //阻止默认行为 if(event.preventDefalut){
event.preventDefault(); }else{
event.returnValue = false; } }, getRelatedTarget:function(event){ //获取当前目标(mouseover Or mouseout)的时候有效 if(event.getRelatedTarget){
return event.getRelatedTarget; }else if(event.toElement){
return event.toElement; }else if(event.fromElement){
return event.fromElement; }else{
return null; } }, addLoad:function(fun){
//window.onload处理 var nowLoad = window.onload; if(typeof nowLoad!="function"){
window.onload = fun; }else{
window.onload = function(){
nowLoad(); fun(); }; }; } }; //获取对象 function g(id){
if(!id)return; if(typeof id == "string"){
return document.getElementById(id); }; return id; }; //创建容器 function createEle(ele,css,father){
ele = ele || "div"; var obj = document.createElement(ele); if(css){
setStyle(ele,css); }; father = father || document.body; father.appendChild(obj); }; //设置样式 function setStyle(ele,css){
if(!ele)return; var x,y,z,m; z = ele; x = z.style; for(var s in css){
m = css[s]; y = m + "px"; switch(s){
case "w" : x.width = y;break; case "h" : x.height = y;break; case "l" : x.left = y;break; case "t" : x.top = y;break; case "z" : x.zIndex = m;break; case "a" : x.position = m;break; case "d" : x.display = m;break; case "i" : z.innerHTML = m;break; case "bg": x.background = x;break; case "b" : x.border = x ;break; case "d" : x.display = x ;break; case "m" : x.margin = x;break; case "p" : x.padding = x;break; default : x[s] = m; break ; }; }; }; //获取窗口高宽 function getW(){
var client_h,client_w,scrollTop; client_h = document.documentElement.clientHeight || document.body.clientHeight; client_w = document.documentElement.clientWidth || document.body.clientWidth; scrollTop = document.documentElement.scrollTop || document.body.scrollTop; return o = {w:client_w,h:client_h,s:scrollTop}; } //获取CSS function getStyles(ele) {
var style; if (document.defaultView && document.defaultView.getComputedStyle) style = document.defaultView.getComputedStyle(ele, null); else style = ele.currentStyle; return style; }; //随即数 function getRandom(upper,lower){
return Math.floor(Math.random() * (upper - lower + 1) + lower); }; //getElePosition function getElePosition(ele){
if(!ele)return; var left = 0,top = 0; if("getBoundingClientRect" in document.documentElement){
var box = ele.getBoundingClientRect(), doc = ele.ownerDocument, body = doc.body, docElem = doc.documentElement, clientTop = doc.clientTop || body.clientTop || 0, clientLeft = doc.clientLeft || body.clientLeft || 0 , left = box.left + (self.pageXOffset || docElem && docElem.scrollLeft || body.scrollTop) - clientLeft, top = box.top + (self.pageYOffset || docElem && docElem.scrollTop || body.scrollTop) - clientTop; }else{
do{
top+=ele.offsetTop, left+=ele.offsetLeft, ele = elel.offsetParent; }while(ele) }; return {left:left,top:top} }; //getId / getClass / getEle / addClass / removeClass var tool={
$:function(id){
return typeof id==="object"?id:document.getElementById(id) }, $$:function(tagName,oParent){
return(oParent||document).getElementsByTagName(tagName) }, $$$:function(className,tagName,elem){
var i=0,aClass=[], reClass=new RegExp("(^|\\s)"+className+"(\\s|$)"), aElement=tool.$$(tagName||"*",elem||document); for(i=0;i

 

(function () { //brwoser      browser = navigator.userAgent.toLowerCase();      isAndroid = find(browser, "android");      isIphone = find(browser, "iphone");      isIpad = find(browser, "ipad");      isTouch = isIphone || isIpad;    })();            function find(a, b) {      return a.indexOf(b) + 1;    };        function getMS() {      var z = new Date();      return (z.getMinutes() * 60 + z.getSeconds()) * 1000 + z.getMilliseconds()    };

转载于:https://www.cnblogs.com/webstarting/archive/2012/02/14/2351034.html

你可能感兴趣的文章
UStore-自定义JDF文件格式输出
查看>>
(转)理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER
查看>>
2016"百度星"资格赛1002 大数相加
查看>>
Asp.net core 学习笔记 ( Web Api )
查看>>
构造函数(包含this关键字的简单应用)
查看>>
最烦人的正则表达式记忆口诀
查看>>
leetcode Merge Two Sorted Lists
查看>>
[oracle]常用SQL汇总
查看>>
Struts2_day04--课程介绍_Struts2拦截器概述&底层原理_重要的概念
查看>>
我的Java设计模式-工厂方法模式
查看>>
添加支付宝支付按钮,实现捐赠本站
查看>>
SqlDataReader生成动态Lambda表达式
查看>>
leetcode897
查看>>
莫比乌斯函数+莫比乌斯反演
查看>>
90%的用户都不知道手机内部功能
查看>>
CSU 1325: A very hard problem 中南月赛的一道题。
查看>>
设置串行端口的通信参数
查看>>
JPA基础(二)(转)
查看>>
js获取当前浏览器地址栏的链接,然后在链接后面加参数
查看>>
设为首页 收藏(IE可用)
查看>>