2012-12-03 170 views
0

假设下面的代码:IE条件语句不工作(语法错误?)

<div class="content"> 
<div style="background:url(swoosh.jpg) no-repeat; background-size:100% 100%;" class="top"> 
<div style="height:42px; align:center;" id="logo"> 

我的目标是使与背景swoosh.jpg股利是一个简单的分度类=顶部

我尝试过让条件工作,但是由于某种原因(语法?)它不能正常工作。

以下是我已经尝试

<div class="content"> 
<!--[if !IE]> 
<div class="top"> 
<![endif]--> 
<!--[if IE]> 
<div style="background:url(swoosh.jpg) no-repeat; background-size:100% 100%;" class="top"> 
<![endif]--> 

我要指出,我不可以用多联CSS其他任何此应用程序 - 并且有头无访问。

+0

从IE10开始不再支持条件注释。 – Sampson

+0

我给出的解决方案适用于除IE10之外的所有IE浏览器 - 它会恢复为非IE代码,如果您想专门针对IE10,我认为您必须使用JavaScript解决方案:http://www.impressivewebs.com/ ie10-css-hacks/ – WebweaverD

回答

1

我认为这是如何做到你想要什么,但如前所述,它没有做的事情,如果你有其他的选择(在IE9测试过的最好的方式 - IE10没有按使用条件语句“T工作):

<![if !IE]> 
<div class="top"> 
<![endif]> 
<!--[if IE]> 
<div style="background:url(swoosh.jpg) no-repeat; background-size:100% 100%;" class="top"> 
<![endif]--> 

http://jsfiddle.net/APFZh/2/

IE 10定位需要一点点JS:

<![if !IE]><!--<script> 
if (/*@[email protected]*/false) { 
    document.documentElement.className+=' ie10'; 
} 
</script><!--<![endif]--> 

这追加的一类“IE10”的HTML元素,但你可以写任何你想要的文件

http://www.impressivewebs.com/ie10-css-hacks/

+0

处理IE 9和IE 10以及所有其他浏览器的另一种可能方式是使用'<! - [if gte IE 9]> ... ... <! - '。请参阅http://stackoverflow.com/questions/6742087/how-do-i-make-an-else-in-an-ie-html-conditional/15176305#15176305以供参考。 –

0

将div放在class =“top”但在单独的IE样式表中设置不同样式会更好,因为不建议使用内联CSS。

把这个在您的网页的HEAD:

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

至于它不工作我会suggesrt确保你有你的页面的顶部的有效DOCTYPE为IE是关于非常挑剔。

+0

样式表对于当前的应用程序是不可行的,所有的CSS都必须是内联的。 – NRGdallas

+0

至于doctype - 我的页面实际上只是从标记下来,并且不能在其最终形式中具有标题(标题由我的控制之外的系统生成) - 是否有任何其他形式的解决方法? – NRGdallas

1

尝试增加

<!--[if lt IE 7]><html class="ie6"><![endif]--> 
<!--[if IE 7]><html class="ie7"><![endif]--> 
<!--[if IE 8]><html class="ie8"><![endif]--> 
<!--[if gt IE 8]><!--><html><!--<![endif]--> 

到您的HTML文档的顶部。然后使用CSS这样

.content { 
     color:red; 
    } 
.ie6 .content { 
     color:blue; 
    } 
.ie7 .content { 
     color:green; 
    } 

这样你就可以将所有的CSS在一个文件中,你的IE类旁边的非IE类。

看看这个doc保罗爱尔兰

+0

请注意,我不能使用此应用程序的样式表。 – NRGdallas

0

如果有人仍然达到这个页面,想知道的更新,为什么即瞄准犯规在最近的IE浏览器中工作。 IE 10及以后不再支持有条件的评论。从MS官方网站:

有条件的意见

支持已在Internet Explorer中被删除 10标准和怪癖改进互操作性和 符合HTML5模式。

请在这里看到更多的细节:http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx

如果你迫切需要的目标,即,您可以使用此jQuery代码到即添加类,然后使用.IE类在你的CSS的目标,即浏览器。

if ($.browser.msie) { 
$("html").addClass("ie"); 
}