Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

javascript - querySelectorAll alternative for IE

I'm running some javascript which uses this:

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

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.

Is there any alternative?

EDIT: it works fine on Chrome and FF

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

You need to do two things:

  1. Make sure you have a valid doctype, so you can avoid Quirks mode. If you don't have one, add <!DOCTYPE html> to the very top of your code (above the <html> tag).

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

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

    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.

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

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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...