• JavaScript及兼容性笔记


    1. Json to String

    JSON.stringify(jsonobj)//(IE8+,Chrome 1+,FF 3+) 
    //参考 http://www.nowamagic.net/librarys/veda/detail/374

     2. 页面刷新location.reload()和location.href=location.href

      前者相当于页面右键刷新/F5/刷新按钮;后者相当于浏览器地址栏回车。

    3. 0==""

      一定要注意,0=="" 在js里是true的。

    4. 调用iframe

      document.getElementById("fileUpFrame").contentWindow.document.form.file.click();
      fileUpFrame.document.form.file.click();
      frames["fileUpFrame"].document.form.file.click();

    5. 无刷新上传文件

      var fraFileInput=fraForm.file;
      fraFileInput.onchange=function(){
      var fileName=this.value;
      fraForm.submit.click();
      }
      fraFileInput.click();

      注册file的onchange事件一定要在.click代码之前,否则ie有可能会注册不上onchange事件.

    6. IE的键盘事件

      ie6 7 8 在window上注册的键盘事件无效,只能注册到document上;

      ie9+及其他浏览器window.onkeydown事件晚于document.onkeydown事件;

      ie8- document.onkeydown=function (e){没有传入事件对象,需要window.event} 

    7. IE6-7 字符串处理问题

      不支持使用数组的下标方法 txt[n]获取字符,需要使用txt.substr(n,1);

    8. 获取鼠标在div(div#box>div.one*5) 内的偏移

      假设各层都是offset定位元素,并且鼠标事件发生在其中一个div.one上,可以有两种方法
      (1)加法:
      鼠标事件在div.one的偏移+div.one在div#box的偏移
      e.offsetX+(div.one).offsetX   代码大致如下

    function getOffsetInParent(ele,parent){//js ele
        var x=0,y=0;
        while(ele && ele!=parent){
            x+=ele.offsetLeft;
            y+=ele.offsetTop;
            ele=ele.offsetParent;
        }
        return {x:x,y:y};
    }

      (2)减法:
      e.pageX-[(div#box).offsetLeft+div#box相对body的offsetLeft]
      祝:因为pad等移动端的touch事件没有e.offsetX属性,所以兼容性的方案是使用减法。

     9.获取当前视口高度
      document.documentElement.clientHeight 和window.innerHeight(IE9+)。
      绝大多数情况下,上面两种方法的返回值是一样的,但是在chrome中,如果doctype html外面存在script或者其他标签时,documentElement.clientHeight的返回值会>视口高度。

    转载请注明出处:http://www.cnblogs.com/youryida  欢迎留言或qq交流(1140215489)
  • 相关阅读:
    iOS开发UI—Button基础
    iOS开发UI—UIWindow介绍
    第43月第27天 nginx keeplike高可用
    第43月第23天 商品秒杀 乐观锁
    第43月第22天 github 工程 svn checkout ipa瘦身
    第43月第21天 h264文件格式
    第43月第17天 iOS 子线程开启、关闭runloop performSelector
    第43月第15天 nginx负载均衡 redis
    第43月第11天 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange转rgb
    第43月第10天 uiimage写文件
  • 原文地址:https://www.cnblogs.com/youryida/p/3242859.html
Copyright © 2020-2023  润新知