Javascript:根据是否IE7而改变
我想根据浏览器是否为 IE7 更改一行 javascript 代码。以下是任何其他浏览器的代码:
function showHint(myId)
{
document.getElementById(myId).style.display = "inline-block";
}
对于 IE7,我想要 display =“inline”。
我尝试过条件编译(这向我展示了如何检测浏览器),但它不起作用:
function showHint(myId)
{
document.getElementById(myId).style.display = "inline-block";
/*@cc_on
@if(navigator.appVersion.indexOf(“MSIE 7.”)!=-1)
{
document.getElementById(myId).style.display = "inline";
}
@*/
}
非常感谢任何帮助!
编辑:我没有使用 JQuery。
I would like to change a line of my javascript code based on whether the browser is IE7 or not. Here is the code for any other browser:
function showHint(myId)
{
document.getElementById(myId).style.display = "inline-block";
}
For IE7, I want display = "inline".
I've made an attempt at conditional compilation (this showed me how to detect the browser), but it didn't work:
function showHint(myId)
{
document.getElementById(myId).style.display = "inline-block";
/*@cc_on
@if(navigator.appVersion.indexOf(“MSIE 7.”)!=-1)
{
document.getElementById(myId).style.display = "inline";
}
@*/
}
Any help is greatly appreciated!
EDIT: I'm not using JQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
设置由 IE 的行为决定的全局条件注释 解析:
Set a global determined by the behavior of IE's conditional comment parsing:
您需要检查
navigator.userAgent
。如果你使用jQuery,你可以简单地写
You need to check
navigator.userAgent
.If you use jQuery, you can simply write
我认为你可以使用正则表达式来确定MSIE 7:
I think you can use regular expression to determine MSIE 7:
有一个 Jquery 插件检测浏览器版本。我不知道确切的链接...
There is a Jquery Plugin detecting the Browser Version. I do not know the exact Link ...
条件编译应该可以工作。您的错误似乎是您使用花哨的引号来分隔字符串(
“MSIE 7.”
),或者您试图将显示分配给 IE 未知的内容,并且它正在适应它并抛出一个错误。这是该函数的更简洁版本,它不会重复并解决了这个问题:Conditional compilation should work. It seems that your error is that you are using fancy quotes to delimit a string (
“MSIE 7.”
) or that you are attempting to assign display to something unknown to IE and it is having a fit over it and throwing an error. Here is a more concise version of the function that doesn't repeat itself and solves this issue: