Welcome

首页 / 脚本样式 / JavaScript / 谈谈JS中常遇到的浏览器兼容问题和解决方法

今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上;
常遇到的关于浏览器的宽高问题:

//以下均可console.log()实验var winW=document.body.clientWidth||document.docuemntElement.clientWidth;//网页可见区域宽var winH=document.body.clientHeight||document.docuemntElement.clientHeight;//网页可见区域宽//以上为不包括边框的宽高,如果是offsetWidth或者offsetHeight的话包括边框var winWW=document.body.scrollWidth||document.docuemntElement.scrollWidth;//整个网页的宽var winHH=document.body.scrollHeight||document.docuemntElement.scrollHeight;//整个网页的高var scrollHeight=document.body.scrollTop||document.docuemntElement.scrollTop;//网页被卷去的高var scrollLeft=document.body.scrollLeft||document.docuemntElement.scrollLeft;//网页左卷的距离var screenH=window.screen.height;//屏幕分辨率的高var screenW=window.screen.width;//屏幕分辨率的宽var screenX=window.screenLeft;//浏览器窗口相对于屏幕的x坐标(除了FireFox)var screenXX=window.screenX;//FireFox相对于屏幕的X坐标var screenY=window.screenTop;//浏览器窗口相对于屏幕的y坐标(除了FireFox)var screenYY=window.screenY;//FireFox相对于屏幕的y坐标
event事件问题:

//event事件问题document.onclick=function(ev){//谷歌火狐的写法,IE9以上支持,往下不支持;var e=ev;console.log(e);}document.onclick=function(){//谷歌和IE支持,火狐不支持;var e=event;console.log(e);}document.onclick=function(ev){//兼容写法;var e=ev||window.event;var mouseX=e.clientX;//鼠标X轴的坐标var mouseY=e.clientY;//鼠标Y轴的坐标}
DOM节点相关的问题,我直接封装了函数,以便随时可以拿来使用:

//DOM节点相关,主要兼容IE 6 7 8function nextnode(obj){//获取下一个兄弟节点if (obj.nextElementSibling) {return obj.nextElementSibling;} else{return obj.nextSibling;};}function prenode(obj){//获取上一个兄弟节点if (obj.previousElementSibling) {return obj.previousElementSibling;} else{return obj.previousSibling;};}function firstnode(obj){//获取第一个子节点if (obj.firstElementChild) {return obj.firstElementChild;//非IE678支持} else{return obj.firstChild;//IE678支持};}function lastnode(obj){//获取最后一个子节点if (obj.lastElementChild) {return obj.lastElementChild;//非IE678支持} else{return obj.lastChild;//IE678支持};}
document.getElementsByClassName问题:

//通过类名获取元素document.getElementsByClassName("");//IE 6 7 8不支持;//这里可以定义一个函数来解决兼容问题,当然别在这给我提jQuery...//第一个为全局获取类名,第二个为局部获取类名function byClass1(oClass){//全局获取,oClass为你想要查找的类名,没有“.”var tags=document.all?document.all:document.getElementsByTagName("*");var arr=[];for (var i = 0; i < tags.length; i++) {var reg=new RegExp("\b"+oClass+"\b","g");if (reg.test(tags[i].className)) {arr.push(tags[i]);};};return arr;//注意返回的也是数组,包含你传入的class所有元素;}function byClass2(parentID,oClass){//局部获取类名,parentID为你传入的父级IDvar parent=document.getElementById(parentID);var tags=parent.all?parent.all:parent.getElementsByTagName("*");var arr=[];for (var i = 0; i < tags.length; i++) {var reg=new RegExp("\b"+oClass+"\b","g");if (reg.test(tags[i].className)) {arr.push(tags[i]);};};return arr;//注意返回的也是数组,包含你传入的class所有元素; }
获取元素的非行间样式值:

//获取元素的非行间样式值 function getStyle(object,oCss) { if (object.currentStyle) { return object.currentStyle[oCss];//IE }else{ return getComputedStyle(object,null)[oCss];//除了IE } }
设置监听事件:

//设置监听事件 function addEvent(obj,type,fn){//添加事件监听,三个参数分别为 对象、事件类型、事件处理函数,默认为falseif (obj.addEventListener) {obj.addEventListener(type,fn,false);//非IE} else{obj.attachEvent("on"+type,fn);//ie,这里已经加上on,传参的时候注意不要重复加了};}function removeEvent(obj,type,fn){//删除事件监听if (obj.removeEventListener) {obj.removeEventListener(type,fn,false);//非IE} else{obj.detachEvent("on"+type,fn);//ie,这里已经加上on,传参的时候注意不要重复加了};}
元素到浏览器边缘的距离:

//在这里加个元素到浏览器边缘的距离,很实用function offsetTL(obj){//获取元素内容距离浏览器边框的距离(含边框)var ofL=0,ofT=0;while(obj){ofL+=obj.offsetLeft+obj.clientLeft;ofT+=obj.offsetTop+obj.clientTop;obj=obj.offsetParent;}return{left:ofL,top:ofT};}
阻止事件传播:

//js阻止事件传播,这里使用click事件为例document.onclick=function(e){var e=e||window.event;if (e.stopPropagation) {e.stopPropagation();//W3C标准}else{e.cancelBubble=true;//IE....}}
阻止默认事件:

//js阻止默认事件document.onclick=function(e){var e=e||window.event;if (e.preventDefault) {e.preventDefault();//W3C标准}else{e.returnValue="false";//IE..}}
关于EVENT事件中的target:

//关于event事件中的targetdocument.onmouseover=function(e){var e=e||window.event;var Target=e.target||e.srcElement;//获取target的兼容写法,后面的为IEvar from=e.relatedTarget||e.formElement;//鼠标来的地方,同样后面的为IE...var to=e.relatedTarget||e.toElement;//鼠标去的地方}
鼠标滚轮滚动事件:

//鼠标滚轮事件//火狐中的滚轮事件document.addEventListener("DOMMouseScroll",function(event){alert(event.detail);//若前滚的话为 -3,后滚的话为 3},false)//非火狐中的滚轮事件document.onmousewheel=function(event){alert(event.detail);//前滚:120,后滚:-120}
节点加载:

//火狐下特有的节点加载事件,就是节点加载完才执行,和onload不同//感觉用到的不多,直接把js代码放在页面结构后面一样能实现。。document.addEventListener("DOMContentLoaded",function ( ){},false);//DOM加载完成。好像除IE6-8都可以.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。