如何仅在用户使用javascript浏览IE时才能隐藏按钮

时间:2014-12-19 09:02:50

标签: javascript html

这是我的代码..如果用户正在浏览IE,我想隐藏按钮。 我试过这样但是没有用,任何人都可以帮助我。

<!DOCTYPE html>
<html>
<head>

<script> 
    var lang = navigator.systemLanguage; 
    if (lang!='en-US'){
            document.write("Well, this is not internet explorer");
    } else{
            document.write("This is internet explorer");

           document.getElementById('btn1').style.visibility='hidden';
         }
    </script> 

</head>
<body>

<p>This is a paragraph.

</p>

<button class="btn1">Input</button>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

您可以尝试条件评论,不使用javascript:

&#13;
&#13;
<!--[if IE]>
   <button>Not for IE</button>
<![endif]-->
&#13;
&#13;
&#13;

还有更多:

&#13;
&#13;
<!--[if IE]>
According to the conditional comment this is IE<br/>
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is IE 6<br/>
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is IE 7<br/>
<![endif]-->
<!--[if IE 8]>
According to the conditional comment this is IE 8<br/>
<![endif]-->
<!--[if IE 9]>
According to the conditional comment this is IE 9<br/>
<![endif]-->
<!--[if gte IE 8]>
According to the conditional comment this is IE 8 or higher<br/>
<![endif]-->
<!--[if lt IE 9]>
According to the conditional comment this is IE lower than 9<br/>
<![endif]-->
<!--[if lte IE 7]>
According to the conditional comment this is IE lower or equal to 7<br/>
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is IE greater than 6<br/>
<![endif]-->
<!--[if !IE]> -->
According to the conditional comment this is not IE 5-9
<br/>
<!-- <![endif]-->
&#13;
&#13;
&#13;

答案 1 :(得分:0)

有很多方法可以做到这一点,其中有两个:

  1. 使用CSS:
  2. 将以下css类添加到按钮中,并将其隐藏为IE浏览器,如

    <!--[if gt IE 7]>
    <style>.hideBtn{display:none}</style>
    <!--<![endif]-->
    
    1. 使用Javascript:

      window.navigator,通过查找浏览器版本,将其隐藏起来 document.getElementById(&#39; btn1&#39;)。style.display =&#39; none&#39;;

答案 2 :(得分:0)

感谢您的帮助,我找到了解决方案。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9">
<!--[if IE]>
<style>.hideBtn{display:none}</style>
<![endif]-->
<script> 
    var lang = navigator.systemLanguage; 
    if (lang!='en-US'){
            document.write("Well, this is not internet explorer");
    } else{
            document.write("This is internet explorer");

    }


    </script> 

</head>
<body>

<p>This is a paragraph.

</p>

<button class="hideBtn" id ="hideBtn">Input</button>
</body>
</html>