如何仅在 Firefox 上 show/hide 一个 jquery 脚本

How to show/hide a jquery script only on Firefox

试图仅在 Firefox 浏览器上显示和隐藏 js。 还有一个 question 关于检测所有 Firefox 版本,但它不是我问题的答案所以我问了这个问题。

HTML:

<div id="about_me">
This Text color will be change
</div>

仅在 Firefox 上显示:

$("#about_me").addClass("red");

我已经试过了,但没有用:

<!--[if Gecko ]>
$("#about_me").addClass("red");
<![endif]-->

并向其他浏览器显示并在 Firefox 上隐藏:

$("#about_me").addClass("blue");

如何将 JS 显示到不同的浏览器,该文本颜色仅在 Firefox 上为红色,在其他浏览器上为蓝色。

请看这个Fiddle >>
谢谢。

if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
$("#about_me").addClass("red");
}

或者你也可以这样使用

if(navigator.userAgent.indexOf('Firefox') > -1)
{
$("#about_me").addClass("red");
}

尝试使用 navigator.userAgent 来检测浏览器

The userAgent property returns the value of the user-agent header sent by the browser to the server.

The value returned, contains information about the name, version and platform of the browser.

if(navigator.userAgent.toLowerCase().indexOf("firefox") > -1){
    $("#about_me").addClass("red");
}
else{    
    $("#about_me").addClass("blue");    
}

Fiddle

About Naviagate Useragent | MDN

注意:运行 所有浏览器中的 fiddle。我检查了 chrome、safari、IE 和 firefox

它应该有效:

HTML:

<br>
<div id="about_me">
     This Text color will be change
</div>e here

CSS:

.red { color: red; }
.blue { color: blue; }

JS:

var userAgent = navigator.userAgent.toLocaleLowerCase();  

if(userAgent.indexOf('firefox') > -1) {
    $("#about_me").addClass("red");
} else {
    $("#about_me").addClass("blue"); 
}

Fiddle

http://jsfiddle.net/pyg9ynb5/2/