检查浏览器是否支持JavaScript中的document.querySelectorAll [英] Check if the browser supports document.querySelectorAll in JavaScript

查看:88
本文介绍了检查浏览器是否支持JavaScript中的document.querySelectorAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在虽然大多数现代浏览器支持 document.querySelectorAll(),但您可能会遇到旧版Internet Explorer的问题。检查浏览器是否支持函数的显而易见的方法是:

Now although most modern browser support document.querySelectorAll(), you may run into problems with older versions of Internet Explorer. The obvious way of checking if the browser supports a function would be:

if(document.querySelectorAll){
    //some random code
}

但据我所知,有些浏览器喜欢(IE8)不支持某些属性,例如' body * '。有没有更好的方法来检查 document.querySelectorAll('body *')是否真的有效?

But from what I understand some browsers like (IE8) don't support certain properties, like 'body *'. Is there a better way to check if document.querySelectorAll('body *') will actually work?

推荐答案

document.querySelectorAll 将抛出任何不受支持的选择器,因此您只需使用 try-catch 阻止。

document.querySelectorAll will thrown on any unsupported selector so you can simply use a try-catch block.

这篇关于检查浏览器是否支持JavaScript中的document.querySelectorAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆