条件注释仅适用于样式表吗?
条件注释仅适用于 CSS 样式和样式表链接,还是可以应用于所有 HTML/JS。我之所以这么问,是因为如果用户使用的浏览器中已知某些功能无法像其他浏览器那样工作/运行,我想显示特定消息?
如果没有,是否可以通过 PHP + 正则表达式来获取用户正在使用的确切浏览器,因为 $_SERVER['HTTP_USER_AGENT']
似乎返回一个包含多个用户代理名称的字符串?
Do conditional comments only work for CSS styles and stylesheet links, or can they be applied to all 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你的意思是这样的:
是的,你也可以在 HTML 中使用它(例如,它通常用于有条件地添加一些额外的 CSS 以修复 IE6)。
但我不会依赖于此来告诉用户升级过时的浏览器。相反,我会通过检查
$_SERVER['HTTP_USER_AGENT']
在服务器端执行此操作。浏览器识别并不直接,因为浏览器倾向于按照自己喜欢的方式格式化用户代理;有许多库使用比较列表等,以便通过解析用户代理字符串来尝试找出实际上是浏览器/版本/操作系统。
If you mean stuff like:
yes, you can use this in HTML too (eg. it is often used to conditionally add some extra CSS for IE6 fixes).
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']
.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.
条件注释在标记中“起作用”,您可以将它们包裹在 CSS、HTML 和 JavaScript 中。只需用 [if !IE] 包装非 IE 的任何内容
conditional comments "work" in markup, you can wrap them around CSS, HTML and JavaScript. Just wrap whatever for non IE with [if !IE]