msgbartop
PHP源码分析,Zend引擎分析,Web相关技术研究,Web技术分享–左手代码 右手诗
msgbarbottom

21 Jan 10 IE下var的重要性的又一佐证

一个朋友问了一个js问题, 一段看不出有任何问题的代码, 在ie下报错:”object doesn’t support this property or method”.

function foo(obj) {
   productTree = obj.toString();
   document.getElementById('productTree').innerHTML = productTree;
}

开始, 还以为错误是指obj的toString方法, 绕了半天弯路, 无果..

后来,注意到变量名是productTree没有用var申明, 加上getElementById(‘productTree’)说明有个id为productTree的元素, 并且我们知道在IE下可以直接通过id获取DOM元素的引用, so~

所以啊, 局部变量一定要用var申明, 不仅仅是因为不用var会成全局变量, 更因为在IE下, 有可能会出现这种, 让人很莫名的错误….

Related Posts:

Tags: , ,

Reader's Comments

  1. |

    在IE下就是这样的痛苦,不过加var来声明变量应该规范点。

  2. |

    难道window下的所有变量ie都认为是dom咯,该死的ie

  3. |

    这是ie的老问题了,应该规范使用var声明变量。

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word