Javascript 基于IE文档模式加载css样式表

Javascript 基于IE文档模式加载css样式表,javascript,html,css,internet-explorer,internet-explorer-9,Javascript,Html,Css,Internet Explorer,Internet Explorer 9,我正在建立一个网站,我想根据文档模式包括我的样式表的不同版本浏览器处于非浏览器模式 例如documentmode=ie8,我可能想加载main_ie8.css,但如果documentmode=ie9,我可能想加载main_ie9.css 我有很多用户在兼容模式下运行IE9。这会将文档模式默认为ie7标准。我使用以下元标记强制文档模式符合IE9中的IE9标准: <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

我正在建立一个网站,我想根据文档模式包括我的样式表的不同版本浏览器处于非浏览器模式

例如documentmode=ie8,我可能想加载main_ie8.css,但如果documentmode=ie9,我可能想加载main_ie9.css

我有很多用户在兼容模式下运行IE9。这会将文档模式默认为ie7标准。我使用以下元标记强制文档模式符合IE9中的IE9标准:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
问题是浏览器模式仍然设置为IE兼容。因此,用户代理仍然设置为IE7


由于服务器端无法确定浏览器运行的文档模式,且条件注释也基于浏览器模式而非文档模式,因此如何基于文档模式而非浏览器模式加载css文件。

使用此代码检测浏览器模式和文档模式

var j = jQuery.noConflict();
j(document).ready(function(){
    /* detect usage of IE Developer Tools using different Mode for Browser and Document */
    if(j.browser.msie) {
        var browserVersion = j.browser.version.slice(0,j.browser.version.indexOf("."));
        var documentVersion = document.documentMode;
        }
    }
});
向sample.php脚本发送ajax“get”调用:

            if (window.XMLHttpRequest)
            {
              xmlhttp=new XMLHttpRequest();
            }
            else
            {
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    //do the settings based on the response from php
                    /*you can echo the css  file name to be picked 

based
 on browser mode or document mode*/
                        var response=xmlhttp.responseText;
                    }
                }


xmlhttp.open("GET","http://<?php echo IP; ?>/yourapp/sample.php?mode="+,true);
            xmlhttp.send();

希望有帮助

您可以运行测试文档模式的条件javascript,然后附加css。比如:

if (BrowserDetect.browser == 'Explorer')
{
    if (document.documentMode == 7 && BrowserDetect.version > 7)
    {
        // user is running IE in compatability mode
        // load special CSS
    }
}
这是从类似帖子的答案中得到的

如果这更像是浏览器支持CSS样式的问题(听起来像),那么您也可以通过加载垫片和多边形填充来解决这一问题,以便在较旧的IE版本中支持CSS3。流行的HTML5 Shiv是一个很好的起点


可能有助于解决您针对的特定问题的其他多边形填充的集合位于

您可以尝试条件注释


IE8文档模式和IE8有什么不同吗?不知道你到底在问什么。我认为IE8中的IE8文档模式和IE9中的IE8文档模式没有任何区别。我可能错了,但我相信页面在这两种情况下应该呈现相同的效果?
<p class="accent">
<!--[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]-->
</p>