什么会导致 Internet Explorer 无法正确处理条件注释?

发布于 2024-12-28 20:39:52 字数 515 浏览 4 评论 0原文

我正在编写 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://" + "..." + "&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2025-01-04 20:39:52

条件 HTML 注释仅适用于 HTML。 JavaScript 不是 HTML。而是在 JS 中使用条件 JS 注释:

var IE = /*@cc_on!@*/false;

(只有 IE 会解释 !,从而有效地使其 true

使用它

if (IE) {
    url = "http://" + "..." + "&var=1";
} else {
    url = "http://" + "..." + "&var=1";
}

然后您可以按如下方式 然而,在 JS 中应该优先考虑检测。

Conditional HTML comments only work in HTML. JavaScript is not HTML. Rather use conditional JS comments in JS:

var IE = /*@cc_on!@*/false;

(only IE will interpret the ! which effectively makes it true)

Then you can use it as follows

if (IE) {
    url = "http://" + "..." + "&var=1";
} else {
    url = "http://" + "..." + "&var=1";
}

Feature detection should however be preferred in JS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文