为什么这段代码在我的测试服务器上有效,但在 jsfiddle 上却无效?

Why does this code work on my test server but not on jsfiddle?

http://jsfiddle.net/KeithDickens/t2t9pvz4/19/

如其所写,它在我的 Intranet 测试服务器上运行良好,但 JSFiddle 却什么都不做。我错过了 JSFiddle 的细微差别吗?

HTML

<input type="button" value="Add More" onclick="nextLine();">
<div id="test1">
    Test1:<input type="text">
    Test2:<input type="text">
    Test3:<input type="text">
</div>

<br />
<br />
<div style="display:none" id="test2" name="test2">
    Test1:<input type="text">
    Test2:<input type="text">
    Test3:<input type="text">
    <input type="button" value="Remove">
</div>
<br />
<br />
<div style="display:none;" id="test3" name="test3">
    Test1:<input type="text">
    Test2:<input type="text">
    Test3:<input type="text">
    <input type="button" value="Remove">
</div>

JavaScript

    var xy = 2;
    var divid = "";

function nextLine() {
    divid = "test" + xy;
    document.getElementById(divid).style.display = 'block';
    xy++;
    }

因为 jsFiddle 在您的 JS 周围添加 window.onload = function() {},这会导致您的 onClick 属性中的函数不再处于全局范围内。

如果您查看左上角,它会默认执行此操作。为了让它工作,你必须 select 不换行 - 在 <body>: http://jsfiddle.net/t2t9pvz4/23/