我想知道是否可以为 Internet Explorer 在 javascript 文件中编写条件 javascript。
即像这样的……
if (is IE7) {
do this } else {
do this instead
});
我知道我可以使用头部的条件注释为 IE 加载一个完全不同的脚本,但我只想更改一小段代码,因此加载一个完全不同的工作表将是一种“昂贵”的方法。
最佳答案
编写 Javascript 时,功能检测 始终是替代浏览器检测 的方法。因此,不要执行 if (IE7)
,而是执行 if (feature)
。
例如,如果您想知道您的浏览器是否支持 getElementsByClassName()
,您可以检查是否存在函数 ( if (document.getElementsByClassName) 而不是检查浏览器版本)
).
请阅读这篇精彩的文章:
Object detection on Quirksmode
If you want to know whether the browser that views your page supports certain objects you want to use in your code, you should never EVER use a browser detect. Sure, you know that this–and–that browser will support your code while such–and–so browser won’t. But how about other browsers, obscure browsers?
关于javascript - 在 Javascript 文件中为 Internet Explorer 编写条件代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6497410/