[英]<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>
元素,然后是其父项,然后是其父项,依此类推,直到document
和window
为止。 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.