javascript - 如何仅在 Firefox 上显示/隐藏 jquery 脚本

标签 javascript jquery firefox browser cross-browser

尝试仅在 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 >>
谢谢。

最佳答案

尝试使用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

关于javascript - 如何仅在 Firefox 上显示/隐藏 jquery 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30636463/

相关文章:

javascript - 如何以编程方式在 Firefox 中打开文件对话框

javascript - 从插件拦截 Firefox 中的 keyup 事件

javascript - 通过 NodeJS 运行 Java 函数

javascript - 图表或表格可以以不同的格式导出吗?

javascript - react js 和 redux 在每条路线之前发送一个 api 调用

firefox - 字体变体 : small-caps rendered wrong for Roboto Condensed in Firefox only

javascript - 当使用 FromEvent RxJS 和 Angular 订阅输入标签值更改时,如何避免使用 'any'?

javascript - 加载/重定向到外部网站时如何添加加载屏幕?

jquery - 如何在不使用ajax的情况下发送带参数的get请求

jquery - 在元素中查找文本字符串并在其周围包裹一些 span 标签