简体   繁体   English

<a>onclick在FF中导致“不是功能”,但IE正常</a>

[英]<a> onclick causes “is not a function” in FF, but IE ok

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". 我的以下代码在IE8中效果很好,但是在FF 3.5中,单击链接会导致错误“ start_upload不是函数”。 What is going on? 到底是怎么回事? (Of course start_upload is a valid function that works fine in IE) (当然,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>

That is because of the way on.... events are handled, or are standardly supposed to be handled. 那是因为正在进行on....事件已被处理,或者通常应该被处理。 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 . onclick内联函数的上下文是其onclick到的<a>元素,然后是其父项,然后是其父项,依此类推,直到documentwindow为止。 Since IE supports referencing items by ID (and FF should, but still don't do it) it works. 由于IE支持按ID引用项目(FF应该,但仍然不这样做),因此它可以工作。 However, it's correct anyways - start_upload is not a function. 但是,无论如何都是正确的start_upload不是函数。 It's an image. 这是一张图片。 What do you intend start_upload(); 您打算怎么做start_upload(); to do, exactly? 去做,准确吗? You can set its src and that's correct, but you cannot invoke an element as a function. 您可以设置其src ,这是正确的,但是您不能将元素作为函数调用。

What's going on is that IE is violating the ECMAScript spec. 发生的是IE违反了ECMAScript规范。 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. 当您在其中执行start_upload ,它会根据是否在()之后将其解析为函数或元素。 In Gecko start_upload in that situation resolves to the element in all cases, and then you're trying to call an element. 在这种情况下,在Gecko中, start_upload在所有情况下都解析为该元素,然后您尝试调用一个元素。

Somehow,your code is running smoothly in Firefox also (assumed that you have define start_upload function). 某种程度上,您的代码也可以在Firefox中顺利运行(假设您已定义start_upload函数)。 Here is my code which works fine. 这是我的代码,可以正常工作。 Also see, you have close </a> two times (that should not be a problem). 另请参见,您已经两次关闭</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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选项OnClick在IE中不起作用? 但是在FF工作 - options OnClick not working in IE? But working in FF 在IE中两次触发onclick事件,但在FF或Chrome中不会触发两次 - onclick event triggers twice in IE but not in FF or Chrome 简单的onclick应用于<a>无法正常工作的IE和FF</a> - simple onclick applied on <a> not working IE and FF 为什么调用函数onclick在Chrome和FF中不能一直工作,但在IE中工作? - Why does calling a function onclick not work all the time in Chrome and FF, but works in IE? IE中多余的文本(锯齿状)文字-看上去很糟糕-但在FF和Chrome中可以正常显示 - Text extra aliased(jagged) in IE - looks terrible - but OK in FF and Chrome HTML表格:合并的单元格:FF和IE的高度错误(Chrome正常) - HTML table: merged cells: wrong height on FF and IE (ok Chrome) 垂直对齐:在IE8的td上居中(在IE7 / FF / Chrome中可以) - vertical-align: middle on td in IE8 (ok in IE7/FF/Chrome) onClick功能导致Chrome刷新 - onClick function causes refresh on Chrome Onclick 功能在 IE 11 中不起作用 - Onclick function not work in IE 11 IE中的表单元素“未定义”,因此无法访问。 在FF和Chrome中可以正常运行 - Can't access form element in IE because it 'is undefined'. Works ok in FF and Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM