How can I hide a div with javascript if the browser is firefox only?
如果浏览器只是firefox,我如何用javascript隐藏div?
6 个解决方案
#1
8
To check Firefox browser
检查Firefox浏览器
//Javascript
var FIREFOX = /Firefox/i.test(navigator.userAgent);
if (FIREFOX) {
document.getElementById("divId").style.display="none";
}
<!-- HTML-->
<div id="divId" />
#2
7
Just check a FF-specific JavaScript property. E.g.
只需检查特定于FF的JavaScript属性即可。例如。
var FF = (document.getBoxObjectFor != null || window.mozInnerScreenX != null);
if (FF) {
document.getElementById("divId").style.display = 'none';
}
This is called feature detection which is preferred above useragent detection. Even the jQuery $.browser
API (of which you'd have used if ($.browser.mozilla)
for) recommends to avoid useragent detection.
这被称为特征检测,其优先于使用检测。即使是jQuery $ .browser API(你曾经使用过它($ .browser.mozilla))也建议避免使用useragent。
#3
2
“Is the browser Firefox” is almost always the wrong question. Sure, you can start grovelling through the User-Agent
string, but it's so often misleading that it's not worth touching except as a very very last resort.
“浏览器Firefox”几乎总是错误的问题。当然,你可以通过用户代理字符串开始卑躬屈膝,但它常常会产生误导,除非作为最后的手段,否则不值得触摸。
It's also a woolly question, as there are many browsers that are not Firefox, but are based around the same code so are effectively the same. Is SeaMonkey Firefox? Is Flock Firefox? Is Fennec Firefox? Is Iceweasel Firefox? Is Firebird (or Phoenix!) Firefox? Is Minefield Firefox?
这也是一个愚蠢的问题,因为有许多浏览器不是Firefox,但基于相同的代码,所以实际上是相同的。是SeaMonkey Firefox吗?是Flock Firefox吗?是Fennec Firefox吗?是Iceweasel Firefox吗? Firebird(或Phoenix!)Firefox?是Minefield Firefox吗?
The better route is to determine exactly why you want to treat Firefox differently, and feature-sniff for that one thing. For example, if you want to circumvent a bug in Gecko, you could try to trigger that bug and detect the wrong response from script.
更好的途径是确定您想要以不同方式对待Firefox的原因,以及针对这一点的功能嗅探。例如,如果您想绕过Gecko中的错误,您可以尝试触发该错误并从脚本中检测错误的响应。
If that's not possible for some reason, a general way to sniff for the Gecko renderer would be to check for the existence of a Mozilla-only property. For example:
如果由于某种原因这是不可能的,那么嗅探Gecko渲染器的一般方法是检查是否存在仅Mozilla属性。例如:
if ('MozBinding' in document.body.style) {
document.getElementById('hellononfirefoxers').style.display= 'none';
}
edit: if you need to do the test in <head>
, before the body
or target div
are in the document, you could do something like:
编辑:如果您需要在中进行测试,在正文或目标div在文档中之前,您可以执行以下操作:
<style type="text/css">
html.firefox #somediv { display: none }
</style>
<script type="text/javascript">
if ('MozBinding' in document.documentElement.style) {
document.documentElement.className= 'firefox';
}
</script>
#4
2
if(document.body.style.MozTransform!=undefined) //firefox only
#5
1
function detectBrowser(){
....
}
hDiv = .... //getElementById or etc..
if (detectBrowser() === "firefox"){
hDiv.style.display = "none"
}
#6
0
You might try Rafeal Lima's CSS Browser Selector script. It adds a few classes to the HTML element for OS, browser, js support, etc. You can then use these classes as hooks for further CSS and/or JS. You might write a CSS (or jQuery) selector like html.gecko div.hide-firefox
once the script has run.
您可以尝试Rafeal Lima的CSS浏览器选择器脚本。它为OS,浏览器,js支持等HTML元素添加了一些类。然后,您可以将这些类用作更多CSS和/或JS的钩子。一旦脚本运行,您可以编写一个CSS(或jQuery)选择器,如html.gecko div.hide-firefox。
#1
8
To check Firefox browser
检查Firefox浏览器
//Javascript
var FIREFOX = /Firefox/i.test(navigator.userAgent);
if (FIREFOX) {
document.getElementById("divId").style.display="none";
}
<!-- HTML-->
<div id="divId" />
#2
7
Just check a FF-specific JavaScript property. E.g.
只需检查特定于FF的JavaScript属性即可。例如。
var FF = (document.getBoxObjectFor != null || window.mozInnerScreenX != null);
if (FF) {
document.getElementById("divId").style.display = 'none';
}
This is called feature detection which is preferred above useragent detection. Even the jQuery $.browser
API (of which you'd have used if ($.browser.mozilla)
for) recommends to avoid useragent detection.
这被称为特征检测,其优先于使用检测。即使是jQuery $ .browser API(你曾经使用过它($ .browser.mozilla))也建议避免使用useragent。
#3
2
“Is the browser Firefox” is almost always the wrong question. Sure, you can start grovelling through the User-Agent
string, but it's so often misleading that it's not worth touching except as a very very last resort.
“浏览器Firefox”几乎总是错误的问题。当然,你可以通过用户代理字符串开始卑躬屈膝,但它常常会产生误导,除非作为最后的手段,否则不值得触摸。
It's also a woolly question, as there are many browsers that are not Firefox, but are based around the same code so are effectively the same. Is SeaMonkey Firefox? Is Flock Firefox? Is Fennec Firefox? Is Iceweasel Firefox? Is Firebird (or Phoenix!) Firefox? Is Minefield Firefox?
这也是一个愚蠢的问题,因为有许多浏览器不是Firefox,但基于相同的代码,所以实际上是相同的。是SeaMonkey Firefox吗?是Flock Firefox吗?是Fennec Firefox吗?是Iceweasel Firefox吗? Firebird(或Phoenix!)Firefox?是Minefield Firefox吗?
The better route is to determine exactly why you want to treat Firefox differently, and feature-sniff for that one thing. For example, if you want to circumvent a bug in Gecko, you could try to trigger that bug and detect the wrong response from script.
更好的途径是确定您想要以不同方式对待Firefox的原因,以及针对这一点的功能嗅探。例如,如果您想绕过Gecko中的错误,您可以尝试触发该错误并从脚本中检测错误的响应。
If that's not possible for some reason, a general way to sniff for the Gecko renderer would be to check for the existence of a Mozilla-only property. For example:
如果由于某种原因这是不可能的,那么嗅探Gecko渲染器的一般方法是检查是否存在仅Mozilla属性。例如:
if ('MozBinding' in document.body.style) {
document.getElementById('hellononfirefoxers').style.display= 'none';
}
edit: if you need to do the test in <head>
, before the body
or target div
are in the document, you could do something like:
编辑:如果您需要在中进行测试,在正文或目标div在文档中之前,您可以执行以下操作:
<style type="text/css">
html.firefox #somediv { display: none }
</style>
<script type="text/javascript">
if ('MozBinding' in document.documentElement.style) {
document.documentElement.className= 'firefox';
}
</script>
#4
2
if(document.body.style.MozTransform!=undefined) //firefox only
#5
1
function detectBrowser(){
....
}
hDiv = .... //getElementById or etc..
if (detectBrowser() === "firefox"){
hDiv.style.display = "none"
}
#6
0
You might try Rafeal Lima's CSS Browser Selector script. It adds a few classes to the HTML element for OS, browser, js support, etc. You can then use these classes as hooks for further CSS and/or JS. You might write a CSS (or jQuery) selector like html.gecko div.hide-firefox
once the script has run.
您可以尝试Rafeal Lima的CSS浏览器选择器脚本。它为OS,浏览器,js支持等HTML元素添加了一些类。然后,您可以将这些类用作更多CSS和/或JS的钩子。一旦脚本运行,您可以编写一个CSS(或jQuery)选择器,如html.gecko div.hide-firefox。