如何根据浏览器类型加载不同的CSS。 我想在asp.net中为IE和Firefox加载不同的CSS。 我使用的是IE8及以上版本和Firefox 3及以上版本。 请帮助我。
Request.Browser可以提供完整的浏览器信息,您可以在此处检查版本、浏览器名称和类型等。
if(Request.Browser.Browser == "IE")
{
HtmlLink css = new HtmlLink();
css.Href = ResolveClientUrl("~/style/StyleSheet.css");
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";
Page.Header.Controls.Add(css);
}
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="styles/browser.css" />
<![endif]-->
您的主CSS应该是大多数浏览器(包括Firefox)支持的CSS。然后,您可以使用HTML条件语句来加载IE特定的样式表。
<!--[if gt IE 7]>
According to the conditional comment this is Internet Explorer greater than IE8<br />
<link rel="stylesheet" type="text/css" href="IEgreatethan7.css">
<![endif]-->
或者如果您想更具体一些
<!--[if IE 8]>
According to the conditional comment this is Internet Explorer equal to IE8<br />
<link rel="stylesheet" type="text/css" href="IE8.css">
<![endif]-->
<!--[if IE 7]>
<link href="style-ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
如果你在谷歌上搜索你的问题,你会找到答案:
客户端(JavaScript):
http://tycoontalk.freelancer.com/javascript-forum/101813-loading-different-css-based-on-browser.html
服务器端(asp.net):http://msdn.microsoft.com/en-us/library/3yekbd5b.aspx
同时在stackoverflow上搜索: 浏览器检测