nkt*_*nkt 4 html jquery internet-explorer browser-detection
我有一个jQuery代码,适用于Chrome/Mozilla但不适用于IE.
if ($("html").hasClass("ie")) {
$(function(){
$('.green-column, .red-column, .grey-column').click(function() {
alert ($(this).attr("data-type"));
});
});
}
else {
$(function(){
$('.green-column, .red-column, .grey-column').click(function() {
$("<div title='Selected Task is:'>" + $(this).attr("data-type") + "</div>").dialog({
modal: true,
resizable: false,
buttons: [
{
text: "OK",
click: function() { $( this ).dialog( "close" ); }
}
]
});
});
});
}
</script>
<!--[if IE 7]> <html lang="en-us" class="ie"> <![endif]-->
<!--[if IE 8]> <html lang="en-us" class="ie"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en-us" class="ie"> <!--<![endif]-->
Run Code Online (Sandbox Code Playgroud)
所以我计划使用IE9/IE10的警报,但我无法区分浏览器.谁能告诉我如何在jQuery/HTML中识别IE9/IE10?
小智 14
可以在不使用库或条件编译的情况下实现:
if (document.addEventListener) {
alert("IE9 or greater");
}
if (window.requestAnimationFrame) {
alert("IE10 or greater");
}
Run Code Online (Sandbox Code Playgroud)
就这样做
if ($.browser.msie && parseInt($.browser.version, 10) > 8) {
alert('IE9 or IE10');
} else {
alert('Non IE9 or IE10');
}
Run Code Online (Sandbox Code Playgroud)
这适用于1.9以下的jQuery版本.如果您使用1.9+,请阅读此主题或考虑使用modernizr.
检测当前活动的IE版本(可能是模拟的)的唯一可靠方法是组合条件编译和document.documentMode检查.
条件编译是可选的,但它允许您永远不会为非IE浏览器运行IE检测脚本.
例如:
if (/*@cc_on !@*/false && (
document.documentMode === 9 || document.documentMode === 10)
) {
// IE 9 or 10 (not 8 or 11!)
document.documentElement.className += ' ie9 ie10';
}
Run Code Online (Sandbox Code Playgroud)
以前的代码对于缩放器是不安全的.如果你要缩小你的代码,把条件编译的东西放在一个字符串中,eval它:
if (eval('/*@cc_on !@*/false') && ( ... )) { ... }
Run Code Online (Sandbox Code Playgroud)