首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

attachEvent不解解决方法

2012-04-04 
attachEvent不解先看一完整代码:scriptlanguage javascript varimgnullfunctionchackSizeAndType()

attachEvent不解
先看一完整代码:
<script       language= "javascript ">    
  var       img=null;    
  function       chackSizeAndType()    
  {    
          if(img)img.removeNode(true);    
          img=document.createElement( "img ");    
          img.attachEvent( "onreadystatechange ",orsc);     问题1
          img.attachEvent( "onerror ",oe);     问题2
          document.body.insertAdjacentElement( "beforeend ",img);    
          img.src=document.product.FilePath.value;    
  }    
  function       oe()    
  {    
          alert( "上传图片类型为:GIF、JPEG/JPG、BMP ");    
  }    
  function       orsc()    
  {    
          if(img.readyState!= "complete ")return       false;    
      else{
    if(img.fileSize   >   102400){
    alert( "您上传的图片超过100K ")
    }
      }    
  }    
  </script>    
  </head>    
   
  <body       leftmargin= "0 "       topmargin= "0 ">    
  <FORM   METHOD=POST   ACTION= " "   NAME= "product ">
    上传图片 <input       type=file       name= "FilePath "       size=20       onpropertychange= "return     chackSizeAndType(); ">      
  </FORM>
问题1和问题2两行中的attachEvent作如何解释,onreadystatechange和onerror又如何解释???请指教,谢谢!!!

[解决办法]
attachEvent
为注册事件,

onreadystatechange
表示状态完成,动作发生
[解决办法]
img.attachEvent( "onreadystatechange ",orsc); //将事件和方法绑定
onreadystatechange是事件,orsc是方法,意思是说当触发onreadystatechange事件的时候调用orsc方法。(问题二同)
onreadystatechange是当属性改变时触发的事件。
onerror是产生错误的时候,触发的事件。
觉得你最好找个资料好好看看。
[解决办法]
attachEvent
将事件和方法绑定

onreadystatechange
属性值发生更改,则调用该方法。
[解决办法]
IE下的吧,FF下不起作用
FF下用addEventListener和removeEventListener
而且事件名称不要加on

附一段IE下绑定事件和移除事件的资料,建议下载“网页制作完全手册”里面这些函数的资料都有,不过只是针对IE的……
attachEvent Method Internet Development Index

--------------------------------------------

Binds the specified function to an event, so that the function gets called whenever the event fires on the object.

Syntax

bSuccess = object.attachEvent(sEvent, fpNotify)
Parameters

sEvent Required. String that specifies any of the standard DHTML Events.
fpNotify Required. Pointer that specifies the function to call when sEvent fires.

Return Value



Boolean. Returns one of the following possible values:

true The function was bound successfully to the event.
false The function was not bound to the event.


detachEvent Method Internet Development Index

--------------------------------------------

Unbinds the specified function from the event, so that the function stops receiving notifications when the event fires.

Syntax

object.detachEvent(sEvent, fpNotify)
Parameters

sEvent Required. String that specifies any of the standard DHTML Events.
fpNotify Required. Pointer that specifies the function previously set using the attachEvent method.

Return Value

No return value.

热点排行