错误:功能应为Internet Explorer [英] Error: Function expected 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
这是小提琴,您可以根据需要复制粘贴它,我真的不知道该怎么做.我不知道为什么它根本不起作用 http://jsfiddle.net/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 http://jsfiddle.net/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 According to this demo, loop is not a function when clicked. It is a property of loop
seams to be like some magic name.<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
中): http://jsfiddle.net/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):
http://jsfiddle.net/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
的函数: http://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
: http://jsfiddle.net/TDWd6/5/
这篇关于错误:功能应为Internet Explorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!