简体   繁体   中英

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

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

As it's written it works fine on my Intranet test server but JSFiddle it does nothing. Am I missing a nuance of 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++;
    }

Because jsFiddle is adding window.onload = function() {} around your JS, which causes the function in your onClick attribute to no longer be in global scope.

If you look on the top left it does this by default. In order for it to work you'll have to select No wrap - in <body> : http://jsfiddle.net/t2t9pvz4/23/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM