x是Javascript FF/Safari中的保留关键字,而不是IE中的保留关键字吗? [英] Is x a reserved keyword in Javascript FF/Safari not in IE?
问题描述
Web应用程序的网页显示一个奇怪的错误.我逐步删除了所有HTML/CSS/JS代码,然后转到下面的基本和简单代码.
A web page of a web application was showing a strange error. I regressively removed all the HTML/CSS/JS code and arrived to the basic and simple code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>test</title>
<script type="text/javascript">
var TestObj =
{
foo: function() {}
}
alert(x); //ok displays "undefined"
var x = TestObj.foo;
var z = TestObj.foo;
</script>
</head><body>
<p onclick='alert(x);'>Click shows function foo!</p>
<img onclick='alert(x);' alt='CRAZY click displays a number in FF/Safari not function foo' src='' style='display: block; width: 100px; height: 100px; border: 1px solid #00ff00;'>
<p onclick='alert(x);'>Click shows function foo!</p>
</body></html>
这很疯狂:单击P元素时,字符串"function(){}"会按预期显示.但是当点击IMG元素时,它会显示一个数字,好像x函数以某种方式从内存中被删除或实例化了(它甚至没有将x显示为"undefined",而是显示为数字).
It's crazy: when clicking on P elements the string "function(){}" is displaied as expected. But when clicking on IMG element it shows a number as if x function got in some way removed from memory or deinstantiated (it does not even show x as "undefined" but as a number).
为了让您快速进行测试,我也在此处上方放置了工作测试.
To let you test it quickly I placed the working test above also here.
这可以在 Firefox 3.6和Safari 4.0.4 上复制.
一切只能在IE7 +上正常运行.
我真的很笨,我想知道 x 是否是JS Firefox/Safari中的保留关键字.感谢任何可以提供帮助的人!
I'm really clueless, I was wondering if x is maybe a reserved keyword in JS Firefox/Safari. Thanks to anyone who could help!
仅供参考:
- 如果将x()替换为z() 一切都完美地工作 浏览器(这对于 我)
- 在src中添加真实图像 属性不能解决问题
- 在img中删除样式无法修复 问题(我为图像赋予了风格 只是为了帮助您点击图片 这样您就可以看到图像边框了
- if you repalce x() with z() everything work prefectly in all browsers (this is even more crazy to me)
- adding a real image in src attribute does not fix the problem
- removing style in img does not fix the problem (i gave style to image only to help you clicking on image thus you can see the imnage border)
推荐答案
您的问题出在可变范围内,而不是保留了"x".图像对象具有名为"x"的属性.您可以使用Chrome的开发者工具查看此内容.当您调用"alert(x);"时在图像对象上,作用域中的"x"是图像上的"x"属性.
Your problem is with variable scope, not that "x" is reserved. An image object has a property named "x". You can see this with Chrome's developer tools. When you call "alert(x);" on the image object, the "x" in scope is the "x" property on the image.
这篇关于x是Javascript FF/Safari中的保留关键字,而不是IE中的保留关键字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!