如何显示浏览器特定的 HTML?
我正在尝试找到一种方法,使用 javascript 或条件注释(或任何需要的东西)向 IE 用户显示一个链接,并向所有其他浏览器显示另一个链接。
基本上......
//pseudo code
<!--[if IE]>
<a href"ie-only.html">click here!</a>
<!--[else]>
<a href"all-other-browsers.html">click here!</a>
<![endif]-->
我不认为这对于条件评论标签是可能的(仅适用于互联网浏览器)。 另外,我不认为有“其他”声明。
有没有办法用 JavaScript 来做到这一点? 请帮忙! 谢谢!
I'm trying to find a way to display one link to an IE user and another link to all other browsers using javascript or conditional comments (or whatever it takes).
Basically...
//pseudo code
<!--[if IE]>
<a href"ie-only.html">click here!</a>
<!--[else]>
<a href"all-other-browsers.html">click here!</a>
<![endif]-->
I don't think this is possible with conditional comment tags (which only work in internet explorer). Plus I don't think there is an "else" statement.
Is there a way to do this with javascript? Please help! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当然可以。 您只需将非 IE 浏览器的内容保留在一个位置,使其成为条件注释子句的一部分,但实际上不在 内。 然后,不了解条件注释的浏览器将可以正常看到内容。 这称为 downlevel-revealed有条件评论。
不幸的是,Microsoft 为您提供的标记是无效的 HTML(甚至不是格式良好的 XML)。 要使其通过测试,您只需要一些额外的“--”:
虽然我必须回应 AnonJr 的非答案,但与其他浏览器相比,您很少需要为 IE 提供完全独立的链接/页面。 如果您正在做一些棘手的事情,例如复杂的 VML 和 ActiveX 在 IE 中与其他浏览器上的 Flash 一起工作,我想这可能是有原因的,但通常在同一基本页面上进行一些 CSS 和脚本修改就足够了。
Sure it is. You just have to leave the content for non-IE browsers in a position such that it's part of a conditional comment clause but not actually inside a <!-- comment -->. Then browsers that don't know about conditional comments will see the content fine. This is known as a downlevel-revealed conditional comment.
Unfortunately the markup Microsoft give you there is invalid HTML (and not even well-formed XML). To make it pass muster you just need a few additional ‘--’s:
Although I have to echo AnonJr's non-answer, in that it's rare you should need a completely separate link/page for IE compared to other browsers. If you're doing something tricky like complex VML and ActiveX work in IE with Flash on other browsers I guess there could be a reason for it, but usually a few CSS and script hacks over the same basic page should suffice.
这不会是流行的答案,但该死的是有人开始发布它的时候了 - 停止使用特定于浏览器的垃圾。 当新版本出现时,你只会让未来的问题永久化。
如果开发人员花费了额外的时间(是的,这需要时间和努力。如果你不能说服你的客户,说明你还不够努力),那么我们就不会看到 IE7“破坏网络”,并且会有IE8 的风波就更少了。
是的,IE 比其他浏览器更不符合标准。 但是,Fx 也缺少标准中的某些内容。 当谈到“标准”时,他们都很糟糕。 但他们都在变得更好。 (速度不同,但都在变得更好。)
首先想想为什么要尝试这样做,并问问自己,当下一个浏览器版本出现并且您必须重新调整浏览器时,您是否真的想处理这个问题检测以及如何处理浏览器 Y 的 X 版本。
[/rant]
编辑: 回答一些指出我没有真正回答问题的明显事实的评论,没有更多信息这个问题让我想知道我们是否不想帮助人们决定 用玻璃瓶或鞋子敲钉子...
This is not going to be the popular answer, but its damn time somebody started posting it - stop with the browser-specific junk. You're only perpetuating future problems when new versions come out.
If developers had taken the additional time (yes, it takes time and hard work. If you can't convince your clients you aren't trying hard enough) then we wouldn't have seen IE7 "break the web" and there would have been even less of a brouhaha with IE8.
Yes, IE is less standards compliant than the others. But, Fx is also missing certain things that are a part of the standard too. They all suck when it comes to "standards". But they are all getting better. (At different rates, but they are all getting better.)
Think first why you are trying to do this, and ask yourself if you really want to deal with this when the next browser version comes out and you have to re-jigger your browser detection and how you handle version X of browser Y.
[/rant]
Edit: To answer some of the comments that point out the obvious fact that I didn't really answer the question, without more information this question makes me wonder if we're not trying to help a person decide to hammer in a nail with a glass bottle or a shoe...
这是 Microsoft 批准的方式:
更多信息请访问 http ://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx。
编辑
此代码隐式保证可以在从 IE 5 开始的所有当前和未来版本的 IE 中工作。对于非 IE 浏览器,该代码的工作原理是依赖于这些浏览器忽略“无意义的”
标签,它们都这样做,而且我从未见过它失败。 对于只使用良好的 HTML 注释的版本,请参阅 bobince 的答案,实际上我更喜欢微软提供的解决方案。
This is the Microsoft-approved way:
More information available at http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx.
Edit
This code is implicitly guaranteed to work in all current and future versions of IE starting with IE 5. For non-IE browsers, the code works by relying on those browsers ignoring the "nonsensical"
<![if !IE]>
tag, which they all do, and I've never seen it fail. For a version that uses nothing but good ol' HTML comments, see bobince's answer, which I actually prefer to the Microsoft-provided solution.我想出的一种方法是:
从 http:// 获取 javascript 代码www.quirksmode.org/js/detect.html 并将其放入
标记中。
然后在您的
标记中使用:
不确定这是否是最简单的方法,但它完成了工作。
One way that I've figured out how to do it:
Get the javascript code from http://www.quirksmode.org/js/detect.html and put it in the
<head>
tag.Then in your
<body>
tag use:Not sure if this is the most simple way to do it but it got the job done.
也许是在黑暗中开枪,但这行得通吗?
它远不如 if/else 语句那么干净/有吸引力,但是......这是我能想到的实现解决方案的最简单方法。 尽管它本身可能充满了问题。
A shot in the dark, maybe, but would this work?
It's nowhere near as clean/attractive as an if/else statement could be, but...it was the easiest way I could think of to implement a solution. Though it may well be fraught with issues all of its own.
将其添加到您的标题中:
然后将您想要的内容添加到 .css 页面中:
来源:http://rafael.adm。 br/css_browser_selector/
可用的浏览器代码 [浏览器]:
Add this to your header :
Then whatever you want to your .css page :
Source : http://rafael.adm.br/css_browser_selector/
Available Browser Codes [browser]:
我没有尝试,但也许你可以在 CSS 上利用 IE 的缺陷。 Eric Meyer 就该主题撰写了这篇文章:欺骗浏览器和隐藏样式。
I didn't try, but maybe you could use IE flaws on CSS. Eric Meyer has written this article on the subject: Tricking Browsers and Hiding Styles.
您始终可以使用 CSS 对特定浏览器隐藏代码。 例如,考虑以下代码:
您可以应用以下 CSS hack,并且适当的链接将显示到适当的浏览器。
You could always use CSS to hide the code from specific browsers. For instance, considering the following code:
You could apply the following CSS hacks, and the appropriate links would be displayed to the appropriate browsers.
IE 支持条件编译,您可以使用它轻松交付仅适用于 IE 的代码,而无需执行用户代理嗅探或功能检测。
IE supports conditional compilation, which you can use to easily deliver IE-only code without needing to perform user agent sniffing or feature detection.