美文网首页
兼容问题总结

兼容问题总结

作者: 升龙无涯 | 来源:发表于2021-04-06 16:37 被阅读0次

获取标签

问题

document.getElementsByClassName('类名')

在低版本e中不兼容。

解决

document.getElementById('id名')
document.getElementsByTagName('标签名')
document.getElementsByName('name属性值')
document.querySelector('css选择器')
document.querySelectorAll('css选择器')

卷去的高度

问题

// 当有文档声明的时候
document.documentElement.scrollTop
document.documentElement.srollLeft
// 没有文档声明的时候
document.body.scrollTop
document.body.scrollLeft

解决

// 获取
var t = document.documentElement.scrollTop || document.body.scrollTop
var l = document.documentElement.srollLeft || document.body.scrollLeft
// 设置
document.documentElement.scrollTop = document.body.scrollTop = 数值
document.documentElement.srollLeft = document.body.scrollLeft = 数值

获取样式

问题

// W3C标准浏览器
window.getComputedStyle(元素)
// 低版本IE中
元素.currentStyle

解决

function getStyle(ele,attr){
    if(window.getComputedStyle){
       return getComputedStyle(ele)[attr]
    }else{
        return ele.currentStyle[attr]
    }
}

事件绑定

问题

// W3C浏览器
ele.addEventListener(事件类型,函数)
// 低版本Ie
ele.attachEvent('on事件类型',函数)

解决

function bindEvent(ele,type,handler){
    if(ele.addEventListener){
        ele.addEventListener(type,handler)
    }else if(ele.attachEvent){
        ele.attachEvent('on'+type,handler)
    }else{
        ele['on'+type] = handler
    }
}

事件解绑

问题

// W3C浏览器
ele.removeEventListener(事件类型,函数)
// 低版本Ie
ele.detachEvent('on事件类型',函数)

解决

function unBind(ele,type,handler){
    if(ele.removeEventListener){
        ele.removeEventListener(type,handler)
    }else if(ele.detachEvent){
        ele.detachEvent('on'+type,handler)
    }else{
        ele['on'+type] = null
    }
}

获取事件对象

问题

// W3C浏览器
元素.on事件类型 = function(e){}
元素.addEventListener(事件类型,fn)
function fn(e){
    
}
// 在低版本IE中
元素.on事件类型 = function(){ window.event }
元素.addEventListener(事件类型,fn)
function fn(){
    window.event
}

解决

元素.on事件类型 = function(e){
    var e = e || window.event
}
元素.addEventListener(事件类型,fn)
function fn(e){
    var e = e || window.event
}

阻止默认行为

问题

// W3C浏览器
元素.on事件类型 = function(e){
    e.preventDefault()
}
// 在低版本IE中
元素.on事件类型 = function(){ window.event.returnValue = false }

解决

元素.on事件类型 = function(e){
    var e = e || window.event
    e.preventDefault?e.preventDefault():e.returnValue=false
}

阻止事件冒泡

问题

// W3C浏览器
元素.on事件类型 = function(e){
    e.stopPropagation()
}
// 在低版本IE中
元素.on事件类型 = function(){ window.event.cancelBubble = true }

解决

元素.on事件类型 = function(e){
    var e = e || window.event
    e.stopPropagation?e.stopPropagation():e.cancelBubble=true
}

获取精准的目标元素

问题

// W3C浏览器
元素.on事件类型 = function(e){
    e.target
}
// 在低版本IE中
元素.on事件类型 = function(){ window.event.srcElement }

解决

元素.on事件类型 = function(e){
    var e = e || window.event
    var target = e.target || e.srcElement;
}

获取键盘码

问题

// W3C浏览器
元素.on事件类型 = function(e){
    e.keyCode
}
// 在低版本火狐中
元素.on事件类型 = function(e){
    e.which
}

解决

元素.on事件类型 = function(e){
    var e = e || window.event
    var keycode = e.keyCode || e.which;
}

相关文章

  • 兼容问题总结

    1.默认的内外边距不同 问题: 各个浏览器默认的内外边距不同 解决: *{margin:0;padding:0;}...

  • 兼容问题总结

    获取标签 问题 在低版本e中不兼容。 解决 卷去的高度 问题 解决 获取样式 问题 解决 事件绑定 问题 解决 事...

  • Javascript常见问题解决方法!

    移动端兼容问题方案总结 rem 淘宝解决方案https://github.com/amfe/lib-flexibl...

  • 前端浏览器兼容问题总结

    前端浏览器兼容问题总结-scy_coder的文章-掘金[https://juejin.cn/post/684490...

  • 兼容性问题

    CSS兼容性问题PC页面布局兼容JS兼容问题移动页面兼容问题 CSS兼容问题.header {_width:100...

  • 页面开发干货(样式类)

    页面开发干货,主要包括:用户体验,性能问题、兼容问题、页面常用、工作问题总结。希望可以帮助大家,一起搞问题。 一、...

  • 浏览器兼容

    什么是浏览器兼容问题 为什么会有浏览器兼容问题 用到的网站 处理兼容问题的思路 渐进增强和优雅降级 stackov...

  • vue2配置axios

    axios配置源码解决兼容问题1解决兼容问题2 npm install axios -s 在main.js中进行以...

  • CSS清除浮动大全共8种方法

    在各种浏览器兼容问题上,这样让清除浮动更难了,下面总结8种清除浮动的方法: 浮动导致的问题:父级标签高度塌陷---...

  • webpack注意事项记录

    以下问题均为webpack@1.x下总结 1. 版本兼容问题 webpack1.x版本一般都与1.x版本的插件匹配...

网友评论

      本文标题:兼容问题总结

      本文链接:https://www.haomeiwen.com/subject/zvtekltx.html