仅为IE9应用CSS,但其他css正在应用

时间:2013-02-07 06:25:34

标签: css internet-explorer-9

我必须将border-radius CSS属性应用于按钮,但仅当浏览器不是Internet Explorer 9.否则我想使用background-image属性。我尝试使用条件注释为IE9应用background-image,但它不起作用(来自“常规”CSS的border-radius属性也应用于IE9,而不是background-image )。

如何根据浏览器版本更改此设置以使其应用所需的CSS?

/*For IE9*/
<!--[if lte IE 9]>
.PopupBtn
{
  background-image: url("../Images/new-btn.png");
  height: 28px;
  width: 99px;
  border-left-width: 0px;
  border-top-style: none;
  border-right-style: none;
  border-bottom-style: none;
  border-left-style: none;
  cursor: pointer;
}
<![endif]-->

/*Style.css(general)*/
.PopupBtn
{
  -moz-box-shadow: inset 0px 2px 1px 0px #0d0d0d;
  -webkit-box-shadow:inset 0px 2px 1px 0px #0d0d0d;
  box-shadow:inset 0px 2px 1px 0px #0d0d0d;
  background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #fffcff), color-stop(1, #000000));
  background:-moz-linear-gradient(center top, #fffcff 5%, #000000 100%);
  filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffcff', endColorstr='#000000');
  background-color:#fffcff;
  -moz-border-radius:22px;
  -webkit-border-radius:22px;
  border-radius:22px;
  display:inline-block;
  color:#fcfcfc;
  font:bold 13px  trebuchet ms;
  text-decoration:none;
  text-shadow:1px 0px 0px #000000;
  min-width:90px;
  height:30px;
  cursor:pointer;
  border-style:none;
}

4 个答案:

答案 0 :(得分:2)

最好使用jQuery。

if ($.browser.msie  && parseInt($.browser.version, 10) == 9)
 $('.PopupBtn').css({'background-image':'url(../Images/new-btn.png)','height':'28px','width':'99px'});

请参阅http://api.jquery.com/css/优势在于您不仅需要使用更少的代码,还可以调整所有内容,而不仅仅是css。这只是一个例子,你必须填写其余部分:)

答案 1 :(得分:1)

IE的条件评论实际上是html评论,所以你不能将它们放在css文件中,它们必须在网页中。在你的网页的某个地方,你有

<!--[if lte IE 9]>
<style>
.PopupBtn
{
background-image: url("../Images/new-btn.png");
height: 28px;
width: 99px;
border-left-width: 0px;
border-top-style: none;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
cursor: pointer;
}
</style>
<![endif]-->

甚至是评论中的外部样式表链接

答案 2 :(得分:1)

可能会为你使用全部:

<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"> </script>

在头标记中使用此java脚本。

答案 3 :(得分:0)

您需要在实际网页上放置 Internet Explorer条件评论,而不是在CSS文件中。

避免添加内联CSS代码。相反,将它们放在自己的CSS文件中。分离CSS文件是个好主意。为IE“黑客”制作一个,为常规样式表制作另一个。

因此,例如,将您的IE特定CSS放在 ie.css 文件中:

<强> ie.css

.PopupBtn {
    background-image: url("../Images/new-btn.png");
    height: 28px;
    width: 99px;
    border-left-width: 0px;
    border-top-style: none;
    border-right-style: none;
    border-bottom-style: none;
    border-left-style: none;
    cursor: pointer;
}

将常规样式表放在 style.css 中。

<head>标记中:

<head>
   <link rel="stylesheet" type="text/css" href="style.css">

   <!-- [if lte IE 9]>
      <link rel="stylesheet" type="text/css" href="ie.css">
   <![endif]-->
</head>

注意:

IE仍将应用您理解的常规样式表中的任何样式。所以, 确保在常规样式表之后应用条件注释和 ie.css 样式表。这样,它可以覆盖您不想要的任何样式。

确保明确覆盖ie.css中不需要的任何样式,否则,它会“流血”并显示在IE中

请参阅下面的JSFiddle链接。如果你在IE 9中运行它,你会看到一个绿色渐变,单词“Hello”为红色。如果您在任何其他浏览器中运行它,您应该看到一个黑色渐变,其中单词“Hello”为白色。

http://jsfiddle.net/mKrRL/