开发者

<a> onclick causes "is not a function" in FF, but IE ok

开发者 https://www.devze.com 2023-03-07 16:46 出处:网络
My code below works great in IE8, but in FF 3.5 clicking the link causes an error that \"start_upload is not a function\".What is going on?(Of course start_upload is a valid function that works fine i

My code below works great in IE8, but in FF 3.5 clicking the link causes an error that "start_upload is not a function". What is going on? (Of course start_upload is a valid function that works fine in IE)

<div align="left">
  <a href="#" onClick="start_upload();"
    onMouseOver="start_upload.src='/Images/Buttons/Upload-glow.gif'" 
    onMouseOut="start_upload.src='/Images/Butto开发者_开发知识库ns/Upload.gif'">
    <img src="/Images/Buttons/Upload.gif" id="start_upload" width="90" height="25" border="0"></a>
  </a>
</div>


That is because of the way on.... events are handled, or are standardly supposed to be handled. The context of the onclick inline function is the <a> element to which it was attached, then its parent, then its parent, so on until document and window. Since IE supports referencing items by ID (and FF should, but still don't do it) it works. However, it's correct anyways - start_upload is not a function. It's an image. What do you intend start_upload(); to do, exactly? You can set its src and that's correct, but you cannot invoke an element as a function.


What's going on is that IE is violating the ECMAScript spec. When you do start_upload there it resolves it to either a function or an element depending on whether it's followed by () or not. In Gecko start_upload in that situation resolves to the element in all cases, and then you're trying to call an element.


Somehow,your code is running smoothly in Firefox also (assumed that you have define start_upload function). Here is my code which works fine. Also see, you have close </a> two times (that should not be a problem).

    <body>
    <div align="left">
      <a href="#" onClick="start_upload();"
        onMouseOver="start_upload.src='/Images/Buttons/Upload-glow.gif'" 
        onMouseOut="start_upload.src='/Images/Buttons/Upload.gif'">
 <img src="/Images/Buttons/Upload.gif" id="start_upload" width="90" height="25" border="0">
      </a>
    </div>
    </body>
    </html>
    <script>
    function start_upload()
    {
    alert ("a");
    }
    </script>
0

精彩评论

暂无评论...
验证码 换一张
取 消