IE的querySelectorAll替代品 [英] querySelectorAll alternative for IE

查看:166
本文介绍了IE的querySelectorAll替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些使用此功能的javascript:

I'm running some javascript which uses this:

controls = document.querySelectorAll("input,textarea,button");

它应可在IE 9(开发人员和客户端使用的版本)上运行.但是在我们的项目中,我们使用了一些非常老的Web组件,它们仅在兼容模式下才能正常工作.而根据我的研究发现,querySelectorAll仅适用于标准模式.

It should work on IE 9 (which is the version that the developers and the client use). But in our project we use some really old web components that only work properly on compatibility mode. And querySelectorAll only works on standards mode from what i found after some research.

还有其他选择吗?

它在Chrome和FF上正常工作

it works fine on Chrome and FF

推荐答案

querySelectorAll在IE9中工作正常.它甚至可以在IE8中使用.

querySelectorAll works fine in IE9. It even works in IE8.

但是听起来您的问题是使它特别在怪癖模式或兼容模式下工作.

But it sounds like your problem is getting it to work specifically in quirks mode or compatibility mode.

如果那是问题,那么...问题是模式,而不是功能.您需要使网站进入标准模式,然后querySelectorAll可以正常工作.

If that's the problem then... well, the problem is the mode, not the feature. You need to get the site into standards mode, and then querySelectorAll will work.

兼容性模式的全部要点是使浏览器模拟IE的旧版本.这样做的主要方法是禁用自该版本以来添加的所有功能.

The whole point of compatiblity mode is to make the browser emulate an older version of IE. The main way it does this is by disabling all the features that have been added since that version.

因此,基本上,您的意思是我已经关闭了浏览器的一半功能,如何使这些功能之一再次起作用?"

So basically what you're saying is "I've switched off half the features in my browser, how can I get one of those features working again?"

显而易见的答案是:重新打开功能.

And the obvious answer is: switch the features back on again.

方法是确保浏览器处于标准模式.

The way to do that is to make sure the browser is in standards mode.

您需要做两件事:

  1. 确保您具有有效的文档类型,因此可以避免使用Quirks模式.如果您没有,请在代码的最顶部(<html>标记上方)添加<!DOCTYPE html>.

<head>元素中的某处添加IE标准模式元标记:

Add the IE standards mode meta tag somewhere in your <head> element:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

这将迫使IE使用其可用的最佳模式-因此IE9将处于IE9标准模式等.

This will force IE to use the best mode it has available -- so IE9 will be in IE9 standards mode, etc.

您应确保以上两项均在您网站的所有页面中.

You should make sure both of the above are in all pages in your site.


您提到您所依赖的旧组件只能在兼容模式下工作.


You mention that you're relying on an old component that only works in compatiblity mode.

基本答案是,您将无法在陷入兼容模式的同一站点中使用querySelectorAll.而且还有很多其他现代浏览器功能,您也会错过.

The basic answer is that you're not going to be able to use querySelectorAll in the same site where you're stuck with compatibility mode. And there's a lot of other modern browser features you'll be missing out on too.

如果可能,您应该尝试升级该组件或将其修复为在现代标准模式下工作.

If possible, you should try to upgrade that component, or fix it to work in modern standards mode.

如果您真的无法退出兼容模式,则说明您很困.

If you really can't get out of compatibility mode, then you are pretty much stuck.

在这种情况下,您唯一真正的选择是切换到使用内置了自己的选择器引擎的jQuery之类的库.

Your only real option in this case is to switch to using a library like jQuery that has its own selector engine built in.

如果您已经在不使用jQuery的情况下编写了自己的网站,则必须重写所有内容以使用它是一个非常丑陋的想法,但这可能是您唯一的选择.

If you've already written your site without using jQuery, then it's a pretty ugly thought to have to rewrite everything to use it, but that's probably the only option you have.

这篇关于IE的querySelectorAll替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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