Javascript 如何在 IE HTML 条件中创建“else”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6742087/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:59:39  来源:igfitidea点击:

How do I make an "else" in an IE HTML conditional?

javascripthtmlcssinternet-explorer-9

提问by TIMEX

<!--[if lt IE 9]>
    This is less then IE9
ELSE
    this is all browsers: firefox, chrome, etc.
<![endif]-->

How do I do this in my HTML? I want to do an "else" ...

如何在我的 HTML 中执行此操作?我想做一个“其他”...

回答by cwallenpoole

You're not looking for an else, you're looking for <![if !IE]> <something or other> <!--[endif]>(note that this is not a comment).

您不是在寻找 else,而是在寻找<![if !IE]> <something or other> <!--[endif]>(请注意,这不是评论)。

<!--[if IE]>
   You're using IE!
<![endif]-->
<![if !IE]>
   You're using something else!
<![endif]>

You can find documentation on the conditional comments here.

您可以在此处找到有关条件注释的文档。

回答by Jhankar Mahbub

I use the following to load resources in IE9 or newer and all other browsers

我使用以下在 IE9 或更新版本和所有其他浏览器中加载资源

<!--[if gte IE 9]><!-->        
    //your style or script
<!--<![endif]-->

This is hard to believe. Look the opening and closing if statement sections are inside comments (so, its not visible to other browsers) but visible to IE.

这很难相信。查看 if 语句部分在注释中的开头和结尾(因此,它对其他浏览器不可见)但对 IE 可见。

回答by Ruut

The solution for your problem is (note the use of <!-- -->):

您的问题的解决方案是(注意使用<!-- -->):

<!--[if lt IE 9]>
  This is less then IE9
<![endif]-->
<!--[if gt IE 8]> <!-- -->
  this is all browsers: IE9 or higher, firefox, chrome, etc.
<!-- <![endif]-->

回答by kennebec

conditional comments can be in scripts as well as in html-

条件注释可以在脚本中也可以在 html 中

/*@cc_on
@if(@_jscript_version> 5.5){
    navigator.IEmod= document.documentMode? document.documentMode:
    window.XMLHttpRequest? 7: 6;

}
@else{
    alert('your '+navigator.appName+' is older than dirt.');
}
@end
@*/

回答by Jason Gennaro

You do not need to do an else. It is implied. So

您无需执行else. 它是隐含的。所以

// put your other stylesheets here

<!--[if lt IE 9]>
    //put your stylesheet here for less than ie9
<![endif]-->

回答by jazzcat

The accepted answer by @cwallenpoole breaks the markup, makes HTML invalid, and breaks Visual Studio's syntax highlight.

@cwallenpoole 接受的答案破坏了标记,使 HTML 无效,并破坏了 Visual Studio 的语法突出显示。

Here's how you keep it clean:

以下是保持清洁的方法:

<!--[if IE]>
    You're using IE!
<![endif]-->
<!--[if !IE]><!-->
    You're not using IE. See, even SO highlights this correctly.
<!--<![endif]-->

回答by Jeff

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en-US" xmlns="http://www.w3.org/1999/xhtml">

<head>
 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
</head>
 <!--[if IE]>
<body style="margin: 38px 0 0 0;">
 <![endif]-->
 <!--[if !IE]>
<body>
 <![endif]-->

  -Content-

 </body>
</html>

This worked for me, after hours of searching. I needed the head room for a pop-up banner, that IE didn't want to animate. it was supposed to hide itself after a few seconds. Using this, I was able to just move the entire page down just enough, but only in IE, which was exactly what i needed!

经过数小时的搜索,这对我有用。我需要一个弹出式横幅的头部空间,IE 不想设置动画。它应该在几秒钟后隐藏自己。使用它,我能够将整个页面向下移动到足够的程度,但仅限于 IE,这正是我所需要的!

Currently only for those who still use IE, I prefer FireFox or Chrome myself.

目前只针对那些还在使用 IE 的人,我自己更喜欢 FireFox 或 Chrome。

Thank you for these letters/symbols in this specific order!

感谢您按特定顺序使用这些字母/符号!