[关闭]
@cpt 2015-05-04T02:58:34.000000Z 字数 3069 阅读 879

JavaScript兼容性代码

checkSheet


浏览器对很多方法的支持存在差异,所以在使用这些方法的时候应该尽可能的进行客户端检测或者进行函数封装。

  1. function matchesSelector(element, selector){
  2. if(element.matchesSelector){
  3. return element.matchesSelector(selector);
  4. }else if(element.msMatchesSelector){
  5. return element.msMatchesSelector(selector);
  6. }else if(element.mozMatchesSelector){
  7. return element.mozMatchesSelector(selector);
  8. }else if(element.webkitMatchesSelector){
  9. return element.webkitMatchesSelector(selector);
  10. }else{
  11. trow new Error("Not supported.");
  12. }
  13. }
  14. if(matchesSelector(document.body,"body.page1")){
  15. //执行操作
  16. }
  1. var i,len,child = element.firstElementChild;
  2. while(child != element.lastElementChild){
  3. processChild(child);//已知是元素
  4. child = child.nextElementSibling;
  5. }
  1. <div class="bd user disabled"></div>
  2. <script type="text/javascript">
  3. //删除disabled类
  4. div.classList.remove("disabled");
  5. //添加current类
  6. div.classList.add("current");
  7. //切换user类
  8. div.classList.toggle("user");
  9. //确定元素中是否包含既定类名
  10. if(div.classList.contains("bd") && !div.classList.contains("disabled")){
  11. //执行操作...
  12. }
  13. </script>
  1. function contains(refNode,otherNode){
  2. //能力检测
  3. if(typeof refNode.contains == "function" && (!client.engine.webkit || client.engine.webkit >= 522)){
  4. return refNode.contains(otherNode);
  5. }else if(refNode.compareDocumentPosition == "function"){
  6. return !!(refNode.compareDocumentPosition(otherNode) & 16)
  7. }else{
  8. var node = otherNode.parentNode;
  9. do{
  10. if(node === refNode){
  11. return true;
  12. }else{
  13. node = node.parentNode;
  14. }
  15. }while(node != null);
  16. return false;
  17. }
  18. }
  1. function converToArray(nodes){
  2. var array = null;
  3. try{
  4. //针对非IE浏览器
  5. array = Array.prototype.slice.call(nodes,0);
  6. }catch(ex){
  7. array = new Array();
  8. for(var i=0,len=nodes.length;i < len;i++){
  9. array.push(nodes[i]);
  10. }
  11. }
  12. return array;
  13. }
  1. function loadStyleString(css){
  2. var style = document.createElement("style");
  3. style.type = "text/css";
  4. try{
  5. style.appendChild(document.createTextNode(css));
  6. }catch(ex){
  7. styke.styleSheet.cssText = css;
  8. }
  9. var head = document.getElementByTagName("head")[0];
  10. head.appendChild(style);
  11. }
  12. //调用
  13. //loadStyleString("body{background-color:red}");
  1. //标准兼容模式关闭
  2. if(document.compatMode == \"BackCompt\"){
  3. cWidth = document.body.clientWidth;
  4. cHeight = document.body.clientHeight;
  5. sWidth = document.body.scrollWidth;
  6. sHeight = document.body.scrollHeight;
  7. sLeft = document.body.scrollLeft;
  8. sTop = document.body.scrollTop;
  9. }else{//开启CSS1Compt
  10. cWidth = document.documentElement.clientWidth;
  11. cHeight = document.documentElement.clientHeight;
  12. sWidth = document.documentElement.scrollWidth;
  13. sHeight = document.documentElement.scrollHeight;
  14. sLeft = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft;
  15. sTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop;
  16. }
  17. /*简化版*/
  18. var pageWidth=window.innerWidth,
  19. pageHeight=window.innerHeight;
  20. //IE不支持window.innerWidth,所以浏览器为IE时变量类型不为number
  21. if(typeof pageWidth!='number'){
  22. //标准兼容模式开启
  23. if(document.compatMode=='CSS1Compat'){
  24. pageWidth=document.documentElement.clientWidth;
  25. pageHeight=document.documentElement.clientHeight;
  26. }
  27. else{
  28. //标准兼容模式关闭的情况
  29. pageWidth=document.body.clientWidth;
  30. pageHeight=document.body.clientHeight;
  31. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注