jQuery 如何使用jQuery检测浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30682247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect a browser using jQuery?
提问by user3592004
I have an issue with a css which is implement by the jQuery, css is work fine in mozilla but not in chrome so need to make a changes in css according to the browser BUT in JQUERY
我有一个由 jQuery 实现的 css 的问题,css 在 mozilla 中工作正常,但在 chrome 中没有,所以需要根据浏览器在 css 中进行更改,但在 JQUERY 中
Anyone help me to get the browser whether it is firefox,chrome or any other.
任何人都可以帮助我获取浏览器,无论是 Firefox、chrome 还是其他任何浏览器。
if ($browserFirefox) {
....code
$('#test').css("margin-top","10%");
}
if ($browserChorme) {
code goes here
}
if ($browserXYZ) {
code goes here
}
回答by 124
Use navigator.userAgent for browser information, like:
使用 navigator.userAgent 获取浏览器信息,例如:
if (navigator.userAgent.search("MSIE") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Chrome") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Firefox") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
//code goes here
}
else if (navigator.userAgent.search("Opera") >= 0) {
//code goes here
}
回答by gennaro esposito
For a better use of the answer of @Nikhil Butani. Happy coding ;)
为了更好地使用@Nikhil Butani 的答案。快乐编码;)
if (navigator.userAgent.search("MSIE") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Chrome") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Firefox") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
//code goes here
}
else if (navigator.userAgent.search("Opera") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("NET") >= 0) {
//code goes here
}
else if (navigator.userAgent.search("Edge") >= 0) {
//code goes here
}
回答by Ashouri
If you need to do changes in css with differente browsers ,you can follow blow codes easily
如果您需要使用不同的浏览器在 css 中进行更改,您可以轻松地跟踪代码
if Webkit (Safari/Chrome) {
#myDIV {margin-top:-3px}
} else if (Firefox) {
#myDIV {margin-top:0px}
} else { // IE and other browsers
#myDIV {margin-top:1px}
}
Although in level of Jquery you can use jquery.browser.Take a look at this http://api.jquery.com/jquery.browser/
虽然在 Jquery 级别你可以使用 jquery.browser.Take 看看这个 http://api.jquery.com/jquery.browser/
回答by AmmarCSE
You can use $.browser.
您可以使用$.browser。
if($.browser.mozzila){
}
However, I strongly recommended you try to come up with a cross-browser solution. User-agent sniffing is not a recommended approach as it can break easily(think multiple versions of same browser).
但是,我强烈建议您尝试提出跨浏览器的解决方案。用户代理嗅探不是推荐的方法,因为它很容易被破坏(想想同一浏览器的多个版本)。
See Downsides of using the navigator object / user-agent sniffing for detecting IE versionsand Browser Detection (and What to Do Instead)
请参阅使用导航器对象/用户代理嗅探来检测 IE 版本和浏览器检测的缺点(以及替代方法)
UpdateFor jQuery > 1.9
更新jQuery > 1.9
This property was removed in jQuery 1.9 and is available only through the jQuery.migrate plugin. Please try to use feature detection instead.
此属性已在 jQuery 1.9 中删除,并且只能通过 jQuery.migrate 插件使用。请尝试改用特征检测。
回答by leapin_leprechaun
You might be as well fixing the issue in your css rather than with scripting. Check out http://browserhacks.com/for browser specific targeting information. You can still use jquery to put a particular class on it, but then declare things differently in your css
您也可以在 css 中解决问题,而不是使用脚本。查看http://browserhacks.com/以获取特定于浏览器的定位信息。您仍然可以使用 jquery 在其上放置特定的类,但随后在您的 css 中以不同的方式声明内容
回答by daniel451
This can be achieved with the jQuery.browser property
这可以通过jQuery.browser 属性来实现
if ($.browser.msie) {
....code
$('#test').css("margin-top","10%");
}
if ($.browser.webkit) {
code goes here
}
if ($.browser.mozilla) {
code goes here
}
Should do the work.
应该做的工作。