错误“功能预期”在IE中:'< input onclick =" start();" ...' [英] Error "Function expected" in IE : '<input onclick="start();" ... '

查看:191
本文介绍了错误“功能预期”在IE中:'< input onclick =" start();" ...'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一段HTML代码:

 < input onclick =start(); type =buttonvalue =Start!/> 

在Firefox和Chrome中,它会调用 start 单击。但是,在IE9中,它会产生错误消息: SCRIPT5002:函数预期。然后我尝试了几种不同的情况:

 <!DOCTYPE html> 
< html>
< head>
< / head>
< body>
< script>
函数start(){
alert(Let's start);

function stop(){
alert(Let's stop);
}
< / script>
< input onclick =start(); type =buttonvalue =Start!/>< br /> <! - 失败:主要问题 - >
< span onclick =start();>开始!< / span>< br /> <! - 工作 - >
< input onclick =stop(); type =buttonvalue =停止!/>< br /> <! - 工作 - >
< input onclick =start(); type =textvalue =Start!/>< br /> <! - 失败 - >
< input onmouseover =start(); type =buttonvalue =Start!/>< br /> <! - 失败 - >
< / body>
< / html>




  1. 原始案例:失败(这就是我来这里的原因。)

  2. 使用 span 而不是 input :成功

  3. 使用另一个函数名称:Success

  4. 使用其他类型:失败

  5. 使用 onmouseover 而不是 onclick :失败

我用调试器,它说 start function start(){alert(Let's start);} 。然后我使用 alert(start),它表示 fileopen



作为测试3的结果,我可以通过更改名称轻松解决问题,但是我想知道为什么会在某些情况下发生此错误,但在其他情况下未发生此错误。 Edit:或者是下一个最好的东西,其他解决方法,而不改变名称

解决方案

start 是IE中 input 元素的一个属性。 (据微软表示,当一个视频剪辑文件我不知道这意味着什么,但它并不重要。)在你的控制台中,你可以通过运行来看到它:

 > document.createElement(input)。start b $ bfileopen

在元素的内联事件处理程序,该元素的所有属性都可用作顶级变量。就好像内联代码在 中运行(( thisParticularElement) 块(其中 thisParticularElement 显然是具有侦听器的元素)。因此,在内联事件代码的上下文中,变量 start 引用元素的 start 属性,它是一个字符串,而不是函数。



只需更改函数名称,或明确使用 window.start


There is a piece of HTML:

<input onclick="start();" type="button" value="Start!"/>

In Firefox and Chrome it invokes start on click. However in IE9 it produces the error message: SCRIPT5002: Function expected. Then I tried several different cases:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
        function start(){
            alert("Let's start");
        }
        function stop(){
            alert("Let's stop");
        }
    </script>
    <input onclick="start();" type="button" value="Start!"/><br/> <!-- Failed : the main issue -->
    <span onclick="start();">Start!</span><br/> <!-- Work -->
    <input onclick="stop();" type="button" value="Stop!"/><br/> <!-- Work -->
    <input onclick="start();" type="text" value="Start!"/><br/> <!-- Failed -->
    <input onmouseover="start();" type="button" value="Start!"/><br/> <!-- Failed -->
</body>
</html>

  1. Original case: Failed (That's why I'm here.)
  2. Use a span instead of input: Success
  3. Use another function name: Success
  4. Use a different type: Failed
  5. Use onmouseover instead of onclick: Failed

I used debugger and it said start is function start(){alert("Let's start");}. Then I used alert(start) and it said fileopen.

As the result of test 3, I can easily work-around by changing the name, but I want to know why this error would happened in some cases but not happened in the others. Edit: Or the next best thing, other work-arounds without changing the names.

解决方案

start is a property of input elements in IE. (According to Microsoft, it indicates, "when a video clip file should begin playing." I have no idea what that means, but it's not important.) In your console, you can see this by running:

> document.createElement("input").start
"fileopen"

In an element's inline event handler, all the properties of that element are available as top-level variables. It's as if the inline code is run inside a with(thisParticularElement) block (where thisParticularElement is the element with the listener, obviously). Therefore, in the context of the inline event code, the variable start refers to the start property of the element, which is a string, not a function.

Simply change the function name, or use window.start explicitly.

这篇关于错误“功能预期”在IE中:'&lt; input onclick =&quot; start();&quot; ...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆