错误:功能应为Internet Explorer

编程入门 行业动态 更新时间:2024-10-12 22:34:04
本文介绍了错误:功能应为Internet Explorer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个不仅在IE中工作的代码.在其他浏览器上一切正常,在IE中发生的错误是

I have a code that doesn't only work in IE. everything works fine on other browsers, the error that occurs in IE is this

Line: 11 Error: Function expected

这是小提琴,您可以根据需要复制粘贴它,我真的不知道该怎么做.我不知道为什么它根本不起作用 jsfiddle/laupkram/TDWd6/

this is the fiddle, you can copy paste it as you wish, I really don't know what do. I have no idea why it doesn't work at all jsfiddle/laupkram/TDWd6/

代码:

<form name="formx"> <input type="text" name="txtMultiplier"> <input type="button" value="LOOP!" onClick="loop()"> </form> <script> function loop(){ var mynumbers = [0,1,2,3,4,5,6,7,8,9,10]; var num = parseInt(document.formx.txtMultiplier.value); document.write("Simulating For Loop<br>"); for(var i = 0; i < mynumbers.length; i++){ var prod = num * mynumbers[i]; document.write(mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>"); } document.write("<br>"); document.write("Simulating Do While<br>"); var i = 0; do{ var prod = num * mynumbers[i]; document.write(mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>"); i++; }while(i < mynumbers.length); document.write("<br>"); document.write("Simulating While<br>"); var i = 0; while(i < mynumbers.length){ var prod = num * mynumbers[i]; document.write(mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>"); i++; } } </script> ​

推荐答案

不知道为什么,但是 loop 看起来像是一个魔术名字.据此演示,单击时循环不是功能.它是< input type ="button"/> 元素的属性,该属性调用循环函数.在 onclick this 中,该元素指向元素本身,而loop是它的属性,等于1.这就是为什么它失败并出现错误的原因.可能的解决方案:将 onclick ="loop()" 更改为 onclick ="window.loop()"

Not sure why, but loop seams to be like some magic name. According to this demo, loop is not a function when clicked. It is a property of <input type="button"/> element which calls a loop function. In onclick this points to an element itself and loop is a property of it equal to 1. That is why it fails with error. Possible fix: change onclick="loop()" to onclick="window.loop()"

例如,此处它开始在IE中运行,但 document.write 销毁所有先前的DOM/JS,并在第一次执行 document.write 后停止执行.

For instance, here it starts working in IE, but document.write destroy all previouse DOM/JS and it stops execution after first document.write execution.

如果您在下面的演示中使用类似的东西会更好(结果存储在临时变量中,然后将其传递到 res div的 innerHTML 中): jsfiddle/TDWd6/5/

It will be better if you will use something like on demo below (results are stored in temporary variable which is next passed to innerHTML of res div): jsfiddle/TDWd6/5/

function loop1(){ var mynumbers = [0,1,2,3,4,5,6,7,8,9,10]; var num = parseInt(document.formx.txtMultiplier.value); var res = "Simulating For Loop<br>"; for(var i = 0; i < mynumbers.length; i++){ var prod = num * mynumbers[i]; res += mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>"; } res += "<br>"; res += "Simulating Do While<br>"; var i = 0; do{ var prod = num * mynumbers[i]; res += mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>"; i++; }while(i < mynumbers.length); res += "<br>"; res += "Simulating While<br>"; var i = 0; while(i < mynumbers.length){ var prod = num * mynumbers[i]; res += mynumbers[i].toString() + " x " + num.toString() + "=" + (prod).toString() + "<br>"; i++; } document.getElementById("res").innerHTML = res; }

此外,由于某种原因,当函数名称为 loop 时,即使此代码在IE中也不起作用(在上面的代码和演示中称为 loop1 ).请参阅具有上述代码的演示,但具有名为 loop 的函数: jsfiddle.净/TDWd6/5/

Also, for some reason, even this code does not work in IE when function name is loop (in code and demo above it is called loop1). See demo with code like above, but with function called loop: jsfiddle/TDWd6/5/

更多推荐

错误:功能应为Internet Explorer

本文发布于:2023-10-10 21:29:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   功能   Explorer   Internet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!