尝试仅在 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");
}
About Naviagate Useragent | MDN
注意:在所有浏览器中运行 fiddle 。我检查了 chrome、safari、IE 和 firefox
关于javascript - 如何仅在 Firefox 上显示/隐藏 jquery 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30636463/