JavaScript:错误“对象不支持此操作”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10266543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:14:14  来源:igfitidea点击:

JavaScript: Error "Object doesn't support this action"

javascripthtml

提问by pop stack

The codefor a counter gives an error

计数器的代码给出了错误

Whereas a similar snippetdoes not

类似的片段没有

I can't figure out any valid reason...

我想不出任何正当理由...

The line under consideration is:

正在考虑的线路是:

<input type=button name="but2" value="stop" onClick="window.clearTimeout(ID);">    

The complete code is:

完整的代码是:

<html>
        <head>
        <script language="JavaScript">
            var counter=0;
            ID=window.setTimeout("start();",2000);
            function start()
            {
                counter++;
                document.forms[0].elements[0].value=counter;
                ID=window.setTimeout("start();",2000);
            }
        </script>
        </head>
        <body>
            <form name="frm1">
                <input type="text" name="timer1">
                <input type="button" name="but1" value="start" onClick="counter=0; start();">
                <input type=button name="but2" value="stop" onClick="window.clearTimeout(ID);">
            </form>
        </body>
</html>

回答by tere?ko

Everything about that code there is wrong. Please try to avoid that source of tutorials in the future.

关于那个代码的一切都是错误的。以后请尽量避免使用该教程来源。

Here is a working script: http://jsfiddle.net/teresko/qTJPx/

这是一个工作脚本:http: //jsfiddle.net/teresko/qTJPx/

List of problem with your script:

您的脚本问题列表:

  • missing doctype
  • language="JavaScript"is deprecated
  • variables IDand counterended up in global scope
  • using html to attach events
  • incorrect use of setTimeout
  • <script>tag used in <head>when DOM is not ready yet
  • 缺少文档类型
  • language="JavaScript"已弃用
  • 变量IDcounter最终进入全局范围
  • 使用 html 附加事件
  • 不正确的使用 setTimeout
  • <script><head>当 DOM 尚未准备好时使用的标签

.. and i don't even want to go over that "similar snippet", it looks like something that by all rights should be dead an buried.

..而且我什至不想讨论那个“类似的片段”,它看起来像任何权利都应该被埋葬的东西。

When you add your JavaScript code, it should be right before the closing </body>tag, because at that stage the DOM is already ready, but page has not begun to render yet.

当你添加你的 JavaScript 代码时,它应该在结束</body>标记之前,因为在那个阶段 DOM 已经准备好了,但页面还没有开始呈现。

I would strongly suggest for you to get some newer materials for learning JavaScript.

我强烈建议你获取一些学习 JavaScript 的新材料。

回答by yanju

Hi I think in this line your getting the error

嗨,我认为在这一行中您收到了错误

ID=window.setTimeout("start();",2000);

Right ?

对 ?

Put this code

把这个代码

var  ID=window.setTimeout("start();",2000);

you'll not get this JavaScript: Error Object doesn't support this actionerror.

你不会得到这个 JavaScript: ErrorObject doesn't support this action错误。

回答by mihai

Use window.startinstead of start for the onClickevent. It may be that IE doesn't create a window context when you use code instead of a function for the handler.

使用window.start而不是开始onClick事件。当您使用代码而不是处理程序的函数时,IE 可能不会创建窗口上下文。