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 正在添加 window.onload = function() {}
围绕您的 JS,这会导致您的 onClick 属性中的函数不再处于全局范围内。
如果您查看左上角,它会默认执行此操作。为了让它工作,你必须选择 No wrap - in <body>
: http://jsfiddle.net/t2t9pvz4/23/
关于javascript - 为什么这段代码在我的测试服务器上有效,但在 jsfiddle 上却无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505101/