2019-11-15 18:36:03 +08:00

58 lines
1.4 KiB
JavaScript

//封装id
function my$(id){
return document.getElementById(id)
}
//获取文本内容兼容代码
function getInnerText(element){
if(element.textContent){
return element.textContent
}else if(element.getInnerText){
return element.innerText
}
}
//获取文本内容兼容代码
function getInnerText(element,value){
if(element.textContent){
element.textContent = value
}else if(element.getInnerText){
element.innerText = value
}
}
//为任意元素绑定事件的兼容
function addEventListener(ele,type,fn){
if(ele.addEventListener){
ele.addEventListener(type,fn,false)
}else if(ele.attachEvent){
ele.attachEvent('on'+type,fn)
}else{
els['on'+type] = fn
}
}
//解绑事件的兼容性
function removeEventListener(ele,type,fn){
if(ele.removeEventListener){
ele.removeEventListener(type,fn,false)
}else if(ele.detachEvent){
ele.detachEvent('on'+type,fn)
}else{
ele['on'+type] = null
}
}
// 事件参数兼容性
var ev = window.event || e
//获取元素的任意一个样式的属性值
function getStyle(ele,attr){
return window.getComputedStyle ? window.getComputedStyle(ele,null)[attr] : ele.currentStyle[attr] || 0
}
//对于页面scrollLeft scrollTop 有需要处理兼容
function getScroll(){
return{
left:window.PageXoffset || document.body.scrollLeft || document.documentElement.scrollLeft || 0,
top:window.PageYoffset || document.body.scrollTop || document.documentElement.scrollTop || 0
}
}