1.删除空白节点
function deleteSpace(node){
var child = node.childNodes();
for(var i = 0; i < child.length;i++){
if(child[i].nodeType === 3 && /^\s+$/.test(child[i].nodeValue)){
child[i].parentNode.removeChild(child[i]);
}
}
return node;
}
2.获取非行内样式
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,1)[attr];
}
3.兼容键盘按键
function getbutton(evt){
var e = evt || window.event;
if(evt){
return event.button;
}else if(window.event){
switch(event.button){
case 1 : return 0;
case 4 : return 1;
case 2 : return 2;
}
}
}
4.兼容滚动条高度
document.onscroll = function(){
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
}
document.scrollTop = function(){
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
}
5.阻止冒泡兼容
function maoPao(evt){
var e = evt || window.event;
if(e.stopPropagation){
e.stopPropagation();
}else{
e.cancelBubble = true;//IE
}
}
6.超链接的兼容
function(event){
var e = event|| window.event;
if(event.preventDefault){
event.preventDefault()
}else{
event.returnValue = false;
}
}
7.滚动条到页面左边的距离
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
8.byClassName的兼容
function byClassName(obj,className){
if(obj.getElementsByClassName){ //支持byClassName
return obj.getElementsByClassName(className);
}else{
var arr = [];
var eles = obj.getElementsByTagName('*'); //获取所有标签
//遍历所有标签,寻找具有指定className的元素
for(var i = 0,len = eles.length;i < len;i ++){
if(eles[i].className == className){
arr.push(eles[i]); //存入数组
}
}
return arr;
}
}
9.获取节点对象中class属性的兼容
function getAttributeFromClass(obj,className){
return obj.getAttribute('class') == null ? obj.getAttribute('className') : obj.getAttribute('class');
}
10.获取事件对象的兼容
var evt = evt || window.event;
11.获取鼠标相对于整个文档的水平及垂直坐标
event.clientX+(document.documentElement.scrollLeft||document.body.scrollLeft);
event.clientY+(document.documentElement.scrollTop||document.body.scrollTop);