仅当浏览器是firefox时,才使用javascript隐藏html元素

Cam*_*ron 7 html javascript firefox

如果浏览器只是firefox,我怎么能用javascript隐藏div?

Buh*_*ndi 8

检查Firefox浏览器

//Javascript
var FIREFOX = /Firefox/i.test(navigator.userAgent);

if (FIREFOX) {
  document.getElementById("divId").style.display="none";
}


<!-- HTML-->
<div id="divId" />
Run Code Online (Sandbox Code Playgroud)

  • Opera不使用**Gecko**渲染引擎,而是使用Presto.它甚至没有用User-Agent字符串中臭名昭着的"Mozilla"标签来标识自己(参见http://www.nczonline.net/blog/2010/01/12/history-of-the-user-agent-字符串/有关此信息).Bobince是对的,由于许可证规定,存在许多无品牌的Firefox版本(在UA字符串中没有"Firefox"):http://en.wikipedia.org/wiki/Mozilla_Firefox#Trademark_and_logo (3认同)

Bal*_*usC 7

只需检查特定于FF的JavaScript属性即可.例如

var FF = (document.getBoxObjectFor != null || window.mozInnerScreenX != null);

if (FF) {
    document.getElementById("divId").style.display = 'none';
}
Run Code Online (Sandbox Code Playgroud)

这被称为特征检测,其优先于使用检测.即使是jQuery $.browserAPI(您已经使用if ($.browser.mozilla)过)也建议避免使用useragent.

  • `mozInnerScreenX`也只能从Firefox 3.6上获得. (3认同)