什么会导致 Internet Explorer 无法正确处理条件注释?
我正在编写 JSP 页面并使用 Tomcat,除了 Firefox 和 Chrome(客户需求)之外,它还需要适用于 IE 7。
在我的程序中,我包含了这两段代码。它适用于非 IE 浏览器。
我的问题是,代码 A 不能在 IE 上正常工作,因为它将它视为注释,而不是它应该读取的条件注释。知道为什么会发生这种情况以及如何解决它吗?
<script type="text/javascript">
...
<!-- CODE A -->
<!--[if IE]>
url = "http://" + "..." + "&var=1";
<![endif]-->
<!-- CODE B -->
<!--[if !IE]> -->
url = "http://" + "..." + "&var=1";
<!-- <![endif]-->
...
</script>
I'm writing JSP pages and using Tomcat, and it needs to work for IE 7 in addition to Firefox and Chrome (client needs).
In my program, I include both pieces of code. It works properly for non-IE browsers.
My problem is that CODE A does not work properly for IE, in that it treats it like a comment rather than a conditional comment that it should be reading. Any idea why this would happen and how to fix it?
<script type="text/javascript">
...
<!-- CODE A -->
<!--[if IE]>
url = "http://" + "..." + "&var=1";
<![endif]-->
<!-- CODE B -->
<!--[if !IE]> -->
url = "http://" + "..." + "&var=1";
<!-- <![endif]-->
...
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
条件 HTML 注释仅适用于 HTML。 JavaScript 不是 HTML。而是在 JS 中使用条件 JS 注释:
(只有 IE 会解释
!
,从而有效地使其true
)使用它
然后您可以按如下方式 然而,在 JS 中应该优先考虑检测。
Conditional HTML comments only work in HTML. JavaScript is not HTML. Rather use conditional JS comments in JS:
(only IE will interpret the
!
which effectively makes ittrue
)Then you can use it as follows
Feature detection should however be preferred in JS.