我试图运行一个基于用户是否运行IE的JQuery函数。原因是因为我所拥有的函数在IE中不起作用,所以当用户使用IE时,我想要运行另一个函数。我正在运行JQuery 1.9.1,所以我不能使用$.browser。我试图使用有条件的注释,但我不确定这是否可行。我尝试在这里实现它,但是它不起作用:http://jsfiddle.net/AsQ4E/
这是HTML
<!--[if IE]>
<input type="button" onclick="javascript:ie();" value="Click Me!" />
<![endif]-->
<!--[if !IE]> -->
<input type="button" onclick="javascript:notIE();" value="Click Me!" />
<!-- <![endif]-->这是JQuery
function ie() {
alert("You are using IE");
}
function notIE() {
alert("You are not using IE");
}我怎么才能让这个起作用?这是最好的办法吗?
编辑:我正在尝试使用SVG铁撬。当用户单击一个按钮时,乌鸦从页面中提取一个SVG并弹出下载。下面是它的链接:http://nytimes.github.io/svg-crowbar/。问题是它在IE中不起作用,所以我只打算在用户使用IE:http://bl.ocks.org/biovisualize/1209499 (这在IE中起作用)时才使用它。我对编程和Javascript不太了解,无法调试IE的问题。
发布于 2013-07-02 14:46:49
使用导航器使用javascript
navigator.userAgent.indexOf('MSIE')现在,如果您真的想要IE和版本,应该从msdn中检查这个脚本。
发布于 2013-07-02 14:44:47
你试过看"navigator.userAgent“吗?你将能够判断它的IE是否非常简单。还有其他有用的浏览器检测脚本,但是如果您只需要对IE进行简单的检查,我将坚持上面的内容。
发布于 2013-07-02 14:51:07
下面是一个使用msdn提供的函数的示例
http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat(RegExp.$1);
}
return rv;
}
function myFunction() {
if (getInternetExplorerVersion() > -1)
ie();
else
notIE();
}
function ie() {
alert("You are using IE");
}
function notIE() {
alert("You are not using IE");
}
</script>
</head>
<body>
<input type="button" onclick="javascript:myFunction();" value="Click Me!" />
</body>
</html>https://stackoverflow.com/questions/17428558
复制相似问题