简体   繁体   English

条件注释仅适用于样式表吗?

[英]Do conditional comments only work for stylesheets?

Do conditional comments only work for CSS styles and stylesheet links, or can they be applied to all HTML/JS. 条件注释仅适用于CSS样式和样式表链接,或者可以将其应用于所有HTML / JS。 I'm asking because I'd like to display a specific message if the user is using a browser in which some functionality is known not to work/function as well as it could in others? 我问是因为我想显示一条特定的消息,如果用户使用的浏览器在某些功能上不能像在其他功能上那样正常工作?

If not, is there anyway, via PHP + regex to get the exact browser that the user is using, as $_SERVER['HTTP_USER_AGENT'] seems to return a string containing several user agent names? 如果不是,是否仍然可以通过PHP +正则表达式来获取用户正在使用的确切浏览器,因为$_SERVER['HTTP_USER_AGENT']似乎返回包含多个用户代理名称的字符串?

conditional comments "work" in markup, you can wrap them around CSS, HTML and JavaScript. 标记中的条件注释“工作”,您可以将它们包装在CSS,HTML和JavaScript周围。 Just wrap whatever for non IE with [if !IE] 只需使用[if!IE]将非IE的内容包装起来

If you mean stuff like: 如果您的意思是:

<!--[if lte IE 6]> ... <![endif]-->

yes, you can use this in HTML too (eg. it is often used to conditionally add some extra CSS for IE6 fixes). 是的,您也可以在HTML中使用它(例如,通常用于为IE6修复有条件地添加一些额外的CSS)。

But I wouldn't rely on this in order to, let's say, tell the user to upgrade the outdated browser. 但是,我不会依靠它来告诉用户升级过时的浏览器。 Instead, I would do that on the server-side by checking the $_SERVER['HTTP_USER_AGENT'] . 相反,我会在服务器端通过检查$_SERVER['HTTP_USER_AGENT']来做到这一点。

The browser recognition isn't straight-forward since browsers tend to format the user-agent as they like; 浏览器的识别并不简单,因为浏览器倾向于按自己的喜好格式化用户代理。 there are many libraries that use comparisons lists etc. in order to try figure out which actually is the browser/version/os by parsing the user-agent string. 有很多使用比较列表等的库,以便通过解析用户代理字符串来找出实际上是浏览器/版本/ os。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM