简体   繁体   English

在Internet Explorer中仅显示Div(所有版本)

[英]Show Div Only in Internet Explorer (All Versions)

With Internet Explorer dropping the usage of conditional statements, it makes me puzzled on how one would apply elements or styles separately and only to IE. 随着Internet Explorer删除条件语句的使用,这使我感到困惑,因为人们如何将元素或样式分别仅应用于IE。 This no longer works with IE10: 这不再适用于IE10:

<!--[if IE]>
<![endif]-->

So here is a jsfiddle of a blue and red box: http://jsfiddle.net/74yK9/1/ 因此,这是一个蓝色和红色框的jsfiddle: http : //jsfiddle.net/74yK9/1/

How would I make the blue box only appear in Internet Explorer (all versions)? 如何使蓝色框仅出现在Internet Explorer(所有版本)中?

You can do this using jQuery, don't forget to include the jQuery Migrate plugin too: 您可以使用jQuery来做到这一点,别忘了也包括jQuery Migrate插件

if ($.browser.msie) {

    // For IE only
    $('#bluebox').show();
} else {

    // all other browsers
    $('#bluebox').hide();
}

Using Javascript, you can use conditional compilation which is only available in IE: 使用Javascript,可以使用仅在IE中可用的条件编译:

if (Function('/*@cc_on return true@*/')()) {
    // Is IE
}

Modified from here: How do I target only Internet Explorer 10 for certain situations like Internet Explorer-specific CSS or Internet Explorer-specific JavaScript code? 从此处进行修改: 在某些情况下,例如特定于Internet Explorer的CSS或特定于Internet Explorer的JavaScript代码,如何仅将Internet Explorer 10作为目标?

So the way to use it is something like: 因此,使用方式类似于:

var isIE = false;
if (Function('/*@cc_on return true@*/')()) {
    isIE = true;
    // or
    document.documentElement.className += " isIE";
}

This sets a boolean in Javascript and/or also adds a isIE class to your <html> element...which will be <html class="isIE"> . 这会在Javascript中设置一个布尔值,并且/或者还会将isIE类添加到您的<html>元素...这将是<html class="isIE"> So that means you can either check the boolean isIE in Javacript, or style things based on the class like: 因此,这意味着您可以检查isIE中的boolean isIE ,也可以根据以下class样式:

html.isIE body {
    color: #111;    /* Only applied for IE*/
}

This uses feature detection (kind of) and doesn't use userAgent sniffing. 这使用功能检测(种类),并且不使用userAgent嗅探。 userAgent sniffing is an option, but isn't reliable and is deprecated/removed in modern jQuery. userAgent嗅探是一个选项,但不可靠,在现代jQuery中已弃用/删除。

A little more info on conditional compilation: http://www.javascriptkit.com/javatutors/conditionalcompile.shtml and http://msdn.microsoft.com/en-us/library/ie/121hztk3%28v=vs.94%29.aspx 有关条件编译的更多信息: http : //www.javascriptkit.com/javatutors/conditionalcompile.shtmlhttp://msdn.microsoft.com/zh-cn/library/ie/121hztk3%28v=vs.94% 29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM