简体   繁体   English

仅在浏览器为Firefox时使用javascript隐藏html元素

[英]Hide an html element using javascript only if browser is firefox

如果浏览器仅是Firefox,如何使用javascript隐藏div?

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" />

Just check a FF-specific JavaScript property. 只需检查特定于FF的JavaScript属性。 Eg 例如

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 (如果要使用if ($.browser.mozilla) ,也会使用该API )建议避免用户代理检测。

“Is the browser Firefox” is almost always the wrong question. “是浏览器Firefox”几乎总是错误的问题。 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. 当然,您可以开始在User-Agent字符串中进行粗加工,但是它常常会引起误解,除非是非常不得已的方法,否则不值得使用。

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. 这也是一个棘手的问题,因为有许多浏览器不是Firefox,但是基于相同的代码,因此实际上是相同的。 Is SeaMonkey Firefox? 是SeaMonkey Firefox吗? Is Flock Firefox? 是Flock Firefox吗? Is Fennec Firefox? 是Fennec Firefox吗? Is Iceweasel Firefox? 是Iceweasel Firefox吗? Is Firebird (or Phoenix!) Firefox? 是Firebird(或Phoenix!)Firefox吗? Is Minefield Firefox? 是Minefield Firefox吗?

The better route is to determine exactly why you want to treat Firefox differently, and feature-sniff for that one thing. 更好的方法是确切确定为什么要区别对待Firefox,并对此功能进行嗅探。 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. 例如,如果您想规避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. 如果由于某种原因无法实现,则嗅探Gecko渲染器的一般方法是检查是否存在仅适用于Mozilla的属性。 For example: 例如:

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: 编辑:如果需要在body或目标div在文档中之前在<head>进行测试,则可以执行以下操作:

<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>
 if(document.body.style.MozTransform!=undefined) //firefox only
function  detectBrowser(){
  ....
}

hDiv = .... //getElementById or etc..

if (detectBrowser() === "firefox"){
  hDiv.style.display = "none"
}

You might try Rafeal Lima's CSS Browser Selector script. 您可以尝试Rafeal Lima的CSS Browser Selector脚本。 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. 它将一些类添加到OS,浏览器,js支持等HTML元素中。然后,您可以将这些类用作其他CSS和/或JS的挂钩。 You might write a CSS (or jQuery) selector like html.gecko div.hide-firefox once the script has run. 脚本运行后,您可以编写类似html.gecko div.hide-firefox的CSS(或jQuery)选择器。

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

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