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

发布于 2024-12-03 21:12:55 字数 213 浏览 6 评论 0原文

条件注释仅适用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-12-10 21:12:55

如果你的意思是这样的:

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

是的,你也可以在 HTML 中使用它(例如,它通常用于有条件地添加一些额外的 CSS 以修复 IE6)。

但我不会依赖于此来告诉用户升级过时的浏览器。相反,我会通过检查 $_SERVER['HTTP_USER_AGENT'] 在服务器端执行此操作。

浏览器识别并不直接,因为浏览器倾向于按照自己喜欢的方式格式化用户代理;有许多库使用比较列表等,以便通过解析用户代理字符串来尝试找出实际上是浏览器/版本/操作系统。

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).

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.

失去的东西太少 2024-12-10 21:12:55

条件注释在标记中“起作用”,您可以将它们包裹在 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文