尝试仅在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&gt;&gt;
感谢。
答案 0 :(得分:2)
尝试使用 navigator.userAgent 检测浏览器
userAgent属性返回发送的用户代理标头的值 通过浏览器到服务器。
返回的值包含有关名称,版本和的信息 浏览器的平台。
if(navigator.userAgent.toLowerCase().indexOf("firefox") > -1){
$("#about_me").addClass("red");
}
else{
$("#about_me").addClass("blue");
}
About Naviagate Useragent | MDN
注意:在所有浏览器中运行小提琴。我检查了chrome,safari,IE和firefox
答案 1 :(得分:1)
if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
$("#about_me").addClass("red");
}
或者你也可以这样使用
if(navigator.userAgent.indexOf('Firefox') > -1)
{
$("#about_me").addClass("red");
}
答案 2 :(得分:1)
应该有效:
HTML:
.red { color: red; }
.blue { color: blue; }
CSS:
var userAgent = navigator.userAgent.toLocaleLowerCase();
if(userAgent.indexOf('firefox') > -1) {
$("#about_me").addClass("red");
} else {
$("#about_me").addClass("blue");
}
JS:
MediaPlayer
<强>小提琴强>