错误:功能预期Internet Explorer

时间:2012-11-26 11:37:25

标签: javascript internet-explorer

我有一个不仅适用于IE的代码。一切在其他浏览器上运行正常,IE中出现的错误就是这个

Line: 11
Error: Function expected
这是小提琴,你可以随意复制粘贴它,我真的不知道是做什么的。我不知道为什么它根本不起作用 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>
​

3 个答案:

答案 0 :(得分:1)

我很确定问题是document.write。当您销毁它时,Internet Explorer不会保留文档中的JavaScript(通过在DOM准备好后通过调用document.open隐式调用document.write),并且您在循环中调用的函数不再存在。

使用createElement / createTextNode / appendChild和朋友代替document.write

答案 1 :(得分:1)

不确定为什么,但loop接缝就像一个神奇的名字。根据this演示,循环不是单击时的功能。它是调用循环函数的<input type="button"/>元素的属性。在onclick this指向一个元素本身,而循环是一个等于1的属性。这就是它失败的原因。可能的解决方法:将onclick="loop()"更改为onclick="window.loop()"

例如,here它开始在IE中工作,但是document.write销毁所有previouse DOM / JS并且在第一次document.write执行后停止执行。

如果您将使用类似于以下演示的内容会更好(结果存储在临时变量中,该变量接下来传递到innerHTML div的reshttp://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.net/TDWd6/5/

答案 2 :(得分:0)

这适用于我本地的ie9。但由于某种原因失败了jsfiddle。一定要喜欢IE。

<form name="formx">
   <input type="text" name="txtMultiplier">
   <input type="button" id="test" value="LOOP!" >
</form>

<script>
document.getElementById('test').onclick = function(){
    loop();
};

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>