js中test做全局变量

    今天想测试一下dom中的空格来着,却发现了一个很隐秘的东西。ie中不能让test作为全局变量。最初的测试代码如下:
    js代码:
   

None.gif < script type = " text/javascript " >
ExpandedBlockStart.gifContractedBlock.gif    window.onload 
=   function () dot.gif {
InBlock.gif    test 
= document.getElementById('test');
InBlock.gif    test.style.backgroundColor 
= "red";
ExpandedBlockEnd.gif}

None.gif
< / script>

       html代码:
      

None.gif < body >
None.gif    
< h1 > DOM简介 < / h1>
None.gif
     < p id = " test " > dom的广泛使用是有道理的,以下是一些原因: < / p>
None.gif
< / body>

    然后在firefox中测试,发现文本“dom的广泛使用是有道理的,以下是一些原因:”出现了红色。即显示了样式。然而在ie7和ie6下测试则都没有效果。
    于是,将test设为局部变量,即将“test=document.getElementById('test')”改为“var test=getElementById('test')”。就能在ie下看到红色文本的效果了。然后我又在将原来的test换成其它变量名,并且还是全局变量。还是能看到效果。翻了翻犀牛书,发现test不是javascript的关键字或保留字,这就肯定了test不能作为ie全局变量名这一结论。所以以后在写js的时候还要好好注意变量的命名。这里就先把js关键字和保留字列出来。o(∩_∩)o...
    以下内容来自《javascript权威指南第五版》

Reserved JavaScript keywords

breakifdoswitchtypeof
caseelseinthisvar
catchfalseinstanceoftHRowvoid
continuefinallynewtruewhile
defaultfornulltrywith
deletefunctionreturn

 

Words reserved for ECMA extensions

abstractdoublegotonativestatic
booleanenumimplementspackagesuper
byteexportimportprivatesynchronized
charextendsintprotectedthrows
classfinalinterfacepublicTRansient
constfloatlongshortvolatile
debugger

 

Other identifiers to avoid

argumentsencodeURLInfiniteObjectString
ArrayErrorisFinitepraseFloatsyntaxError
BooleanescapeisNaNparseInttypeError
DateevalMathRangErrorundefined
decodeURLEvalErrorNaNReferenceErrorunescape
decodeURLComponentFunctionNumberRegExpURLError

转载于:https://www.cnblogs.com/muguaworld/archive/2008/07/14/1242279.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值