<a> onclick causes &#34;is not a function&#34; in FF, but IE ok</a>

时间:2011-05-20 02:31:53

标签: html internet-explorer firefox href

我的代码在IE8中运行良好,但在FF 3.5中单击该链接会导致“start_upload不是函数”的错误。到底是怎么回事? (当然start_upload是一个在IE中工作正常的有效函数)

<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>
  </a>
</div>

3 个答案:

答案 0 :(得分:3)

这是因为处理on....事件的方式,或者标准地应该处理事件。 onclick内联函数的上下文是它所附加的<a>元素,然后是其父项,然后是其父项,依此类推,直到documentwindow。由于IE支持通过ID引用项目(并且FF应该但仍然不这样做)它可以工作。但是,无论如何它都是正确的 - start_upload不是函数。这是一张图片。你打算start_upload();做什么呢?您可以设置其src并且这是正确的,但您不能将元素作为函数调用。

答案 1 :(得分:1)

正在发生的事情是IE违反了ECMAScript规范。当你执行start_upload时,它会将其解析为函数或元素,具体取决于它是否跟随()。在Gecko start_upload中,在所有情况下都解析为元素,然后你试图调用一个元素。

答案 2 :(得分:0)

不知何故,你的代码在Firefox中运行也很顺利(假设你已经定义了start_upload函数)。这是我的代码,工作正常。另请注意,您已经关闭</a>两次(这应该不是问题)。

    <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>