如果是 IE,我如何包含一个 javascript,如果是其他所有内容,我如何包含另一个 javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6742216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I include a javascript if it's IE, and another javascript if everything else?
提问by TIMEX
I want to include <script src="ie.js">
if the browser is IE.
<script src="ie.js">
如果浏览器是 IE,我想包括在内。
Otherwise, include all.js
for all other browsers.
否则,包括all.js
所有其他浏览器。
How can I do this?
我怎样才能做到这一点?
回答by aroth
You can do this using conditional comments. Example:
您可以使用条件注释来做到这一点。例子:
<!--[if IE]>
<script src="ie.js">
<![endif]-->
<!--[if !IE]><!-->
<script src="all.js"></script>
<!--<![endif]-->
回答by Matteo Calò
Conditional comments only work in IE, and are thus excellently suited to give special instructions meant only for IE. They are supported from IE 5 up until IE9 (inclusive).
条件注释仅适用于 IE,因此非常适合给出仅适用于 IE 的特殊指令。从 IE 5 到 IE9(含)都支持它们。