判断是否是IE内核

该代码段定义了一个函数IEVersion()来检测InternetExplorer的文档模式。如果浏览器为IE并且版本可检测,则隐藏id1元素并显示id2元素,同时采用flex布局。否则,显示id1并隐藏id2。这主要用于基于浏览器版本调整页面样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function IEVersion () {
    if (document.documentMode) return document.documentMode;
  }
  if (IEVersion()) {
    document.getElementById('id1').style.display='none'
    document.getElementById('id2').style.display='flex'
    // alert('当前IE浏览器版本号是');
  } else {
    document.getElementById('id1').style.display='flex'
    document.getElementById('id2').style.display='none'
  }
JavaScript 中可以使用一些条件检查来判断浏览器是否为 Internet Explorer (IE) 或者 Edge。由于 Microsoft 对于浏览器标识符的变化,以下是一些推荐的检测方法: 1. **检测 IE 版本** (针对早期版本): ```javascript // 检查 Trident 浏览器内核IE 使用) if (!!window.MSInputMethodContext && !!document.documentMode) { console.log("当前浏览器IE"); } ``` 2. **检测 Edge 特有的 navigator.userAgent 字段** (针对 Edge 和兼容模式): ```javascript // Edge 浏览器有 'Edge/' 前缀 if (!!(navigator.userAgent.indexOf('Edge/') || navigator.userAgent.indexOf('Trident/')) && !navigator.userAgent.toLowerCase().includes('chrome')) { console.log("当前浏览器可能是 Edge"); } ``` 3. **检测 Edge 具体版本** (需要 polyfill,因为原生方法存在兼容性问题): ```javascript // 使用 Modernizr 等库提供的 isEdge 方法,或者第三方库如 @esri/is-ie 来检测 Edge 版本 const isEdge = () => /edge/i.test(navigator.userAgent); if (isEdge()) { console.log("当前浏览器是 Edge"); } ``` 4. **检测兼容模式** (IE11 使用 Edge 模式): ```javascript // 如果是 IE11 的 Edge 模式,会有 'EdgHTML' 或 'Application' 标志 if (typeof window.Edge !== "undefined" && window.StyleMedia) { console.log("当前浏览器可能是 IE11 with Edge mode"); } ``` 请注意,浏览器标识检测可能会随着新版本发布而变化,因此使用上述代码时应保持更新。同时,为了获得最佳用户体验和避免过早优化,现代开发者通常会建议直接忽略对特定旧版浏览器的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值