Javascript Running on JSFiddle but not on a webpage

后端 未结 6 2052
灰色年华
灰色年华 2021-01-26 12:18

Ok so I have a code that would show different forms based on dropdown selection
Here\'s the fiddle to that..

Well its always giving me Test1 which means its not cha

6条回答
  •  孤城傲影
    2021-01-26 12:46

    Jsfiddle is running your script .onload of the window object, without you realizing it. This allows your script to pause on execution until the DOM is ready to be manipulated.

    To have it work in your own environment, you can:

    1. Place the entire script after the HTML you're trying to manipulate (e.g. end of the )
    2. Leave your code above the and run it onload of the window object, e.g.

      window.onload = function () {
          document.getElementById('options').onchange = function () {
              var i = 1;
              var myDiv = document.getElementById(i);
              while (myDiv) {
                  myDiv.style.display = 'none';
                  myDiv = document.getElementById(++i);
              }
              document.getElementById(this.value).style.display = 'block';
          };
       }
      

提交回复
热议问题