zoukankan      html  css  js  c++  java
  • JS中常见的兼容

    1.阻止事件冒泡

      e.stopPropagation()   ||    e.cancelBubble

    function stopBubble(e){
        if (e.cancelBubble) {
            e.cancelBubble = true;
        }else{
            e.stopPropagation();
        }
    }

    2.获取非行间样式

      getcomputedStyle()    参数1:当前对象      参数2:伪类选择器,通常不会使用,用false代替

      currentStyle()              语法:对象.current[attr]

    function getStyle(obj,attr){    //获取非行间样式,obj是对象,attr是值
        if(obj.currentStyle){       //针对ie获取非行间样式       
            return obj.currentStyle[attr];
        }else{
            return getComputedStyle(obj,false)[attr];       //针对非ie
        }
    }

    3.获取className节点的元素

    function elementsByClassName(node, className){
        var res = [];
        //查找node所有的子节点
        var nodes = node.getElementsByTagName("*");
        for(var i = 0; i < nodes.length; i++){
            if(nodes[i].className == className){
                res.push(nodes[i]);
            }
        }
        return res;
        
    }

    4.target事件属性

      var target = e.target || window.event.srcElement

    5.事件中兼容性问题

      var e = event || window.event

    6.scrollTop滚动条距离和clientWidth

    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;

     7.事件监听器

      非IE:    oDiv.addEventListener('click',fn,false);

          oDiv.removeEventListener r('click',fn,false);

      IE下: oDiv.attachEvent();  oDiv.detachEvent();

    8.阻止默认事件

      e.preventDefault();

      window.event.returnValue = false;

        if(e.preventDefault){
            e.preventDefault();
        }else{
            window.event.returnValue = false;
        }
        return false;

    9.键盘事件

      var eve = eve || window.event

      var keyC = eve.keyCode || eve.which

  • 相关阅读:
    对javascript匿名函数的理解(透彻版)
    js操作Cookie
    C# WinForm WebBrowser (一) MSDN资料
    asp.net 生成静态HTML方法
    ASP.NET 操作XML
    c# WinForm 登陆 Program.cs 中代码
    Jquery 将后台返回的字符串转换成Json格式的数据
    获取数据库中的所有用户表、取数据表的描述信息包括描述说明
    asp.net 发送QQ邮件
    设置服务器文件的访问权限
  • 原文地址:https://www.cnblogs.com/wu0379/p/11561554.html
Copyright © 2011-2022 走看看