如果IE忽略/隐藏div

时间:2010-06-30 18:36:36

标签: javascript jquery

我有这样的页面

<html>
<head>

<div id="container">
Hello, this is NON-IE content.
</div>

</head>
</html>

我想要的是停止 Internet Explorer [IE] 来自加载或忽略 <div id="container">&amp; 允许所有其他浏览器 加载它。

不想使用<!--[if IE 7]>

寻找 Javascript 来使用。

我如何实现这一目标?

由于

  • Mandar

4 个答案:

答案 0 :(得分:3)

jquery让这很容易:

if ( $.browser.msie ) {
  $("#container").css("display","none");
}

答案 1 :(得分:3)

对于不想使用jQuery的用户,您只需执行以下操作:

if (/*@cc_on!@*/false) {
    alert('This is Internet Explorer!');
}

http://devoracles.com/the-best-method-to-check-for-internet-explorer-in-javascript

  

我们在那里看到了什么?我宣布了一个新的   变量,称为IE,其中包含   将值注释块后跟   '假'。上面的变量将是   IE理解:var IE =!false,   因为Internet Explorer使用JScript    - 类似Javascript的方言   标准ECMAScript - 而不是   所有人都使用的Javascript   其它浏览器。 JScript可以解析   评论,就像Internet Explorer一样   (参见条件HTML评论帖)。   这是IE的一个独特功能,没有   其他浏览器可以做到这一点,所以   Firefox,Chrome,Safari,Opera等等   将理解上述声明   因为IE = false。

注意:如果任何其他浏览器使用“JScript”,这将通过,但由于JScript是由Microsoft编写的,我认为您是安全的。另一种方法是navigator对象,您可以从中提取应用程序名称。虽然有些应用程序喜欢欺骗这个,但我相信JScript更可靠。

if (navigator.appName == 'Microsoft Internet Explorer') {
    alert('This is Internet Explorer!');
}

编辑:这更多是为了帮助用户检测IE,而不是直接回答用户问题。另外,对于不想使用jQuery的用户,你可以简单地做document.getElementById('container')。style.display ='none'; - 只是想我已经添加了这个,因为我的帖子确实提到了“不使用jQuery”。

答案 2 :(得分:0)

尝试:

if($.browser.msie) $("#container").remove();

答案 3 :(得分:0)

的document.all

一个非常脏的选项是使用IE支持的document.all。

if(document.all){
    document.getElementById("container").style.display = "none";
}

出于某些疯狂的原因,Chrome确实支持document.all,但检查document.all会在其中返回false。

导航

另一个选择是查看导航器对象。

if (navigator && navigator.appName == 'Microsoft Internet Explorer'){
    document.getElementById("container").style.display = "none";
}

对于那些欺骗自己的浏览器,这可能会失败。

比较'功能'

您也可以使用简单的比较。

if('\v' == 'v'){
    document.getElementById("container").style.display = "none";
}

我个人不会这样做,但这是一个很好的检测列表。

相关问题