xuffang / YS_FE

4 stars 0 forks source link

浏览器的兼容问题 #7

Open l644351492 opened 5 years ago

l644351492 commented 5 years ago

写代码的时候,经常会碰到一些浏览器兼容的问题,没有规律,只能碰到的时候记录一点。大家以后如果有碰到浏览器的兼容问题也可以提在这里

  1. new Date()的问题:
    //IE、safari
    new Date('2019-06-01 00:00:00')     //Invalid Date
    //如果IE和safari要能正常识别需要将-替换为/
    new Date('2019/06/01 00:00:00')     //Sat Jun 01 2019 00:00:00 GMT+0800 (中国标准时间)
    //chrome 、firefox
    new Date('2019-06-01 00:00:00')     //Sat Jun 01 2019 00:00:00 GMT+0800 (中国标准时间)

  2. 图片下载问题:
    //在IE中不支持download属性无法直接用<a href="logo.png" download="logo.png">图片</a>的方式下载图片
    //可以用<iframe>标签.生成iframe,src指向图片地址,调用document.execCommand("SaveAs")方法.
    function downloadImg(imgSrc) {
    //如果隐藏的iframe不存在则创建
    if ($("#imgIframe").length === 0){
        $('<iframe style="display:none;" id="imgIframe" name="imgIframe" onload="downloadImg();" width="0" height="0" src="about:blank"></iframe>').appendTo("body");
    }
    $('#imgIframe').attr("src",imgSrc);
    window.frames["imgIframe"].document.execCommand("SaveAs");
    }

  3. innerText兼容IE、Safari、Opera和Chrome而textContent兼容firefox
    if(obj.textContent){    //firefox
    obj.textContent="test";
    }else{
    obj.innerText="test";
    }