Javascript Running on JSFiddle but not on a webpage

断了今生、忘了曾经 提交于 2019-12-02 07:41:17

That is because in the fiddle your code is set to run at onLoad, but in your code its running before the DOM is created.

Wrap your code into a window.onload event like this:

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';
    };
};

Anyway, like @positivew remembered, your code misses the <head> tag. Is semantically correct to put your JS scripts inside it.

The best thing to do when such problem comes works in jsfiddle and not on webpage is to see the source of the fiddle page.

Your source of the fiddle appears as:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>

  <script type='text/javascript' src='/js/lib/dummy.js'></script>
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>
    .hidden 
    {
    display: none;   
    }    

  </style>

  <script type='text/javascript'>//<![CDATA[ 
  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';
  };

   }//]]>  

  </script>
  </head>
  <body>
    <select name="options" id="options">
    <option value="1"> Test1 </option>
    <option value="2">Test2</option>
    </select>

  <div id="1" class="hidden" style="display: block">Test 1</div>
  <div id="2" class="hidden">Test 2</div>
  </body>
  </html>

Paste the above code directly and it will work.

After pasting the code directly then you can remove unncecessary lines like below from the fiddle:

<script type='text/javascript' src='/js/lib/dummy.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

You need a onload function so your code is run after your HTML is loaded. Try this:

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';
    };
}

You can also add the code after all your HTML, before the end of the body tag.

And note that in your post you are missing <head> tags.

You seem to be missing <head> tags.

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 <body>)
  2. Leave your code above the <body> 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';
        };
     }
    
KellyCode

The page is loaded into the DOM from the top down.

Your document.getElementById('options').onchange is being called before the element with id options exists in the DOM.

If you put your script below the divs, it'll work because the divs are now there before the script is called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!