简体   繁体   中英

Why does my code only works in html but does not work when I put it into a Javascript page?

I wanna separate the javascript code from the html page and put it into a separate javascript document, but when I do so, the code does not work anymore. PS I did specify in the html page to retrieve the codes from the separate javascript document.

<script type="text/javascript">
function updateTextInput() {
  var valueHue = document.getElementById('sliderHue').value;
  var valueSaturation = document.getElementById('sliderSaturation').value;
  var valueLightness = document.getElementById('sliderLightness').value;

  document.getElementById('textHue').value = valueHue;
  document.getElementById('textSaturation').value = valueSaturation + "%";
  document.getElementById('textLightness').value = valueLightness + "%";

  var color = "hsl(" + valueHue + ", " + valueSaturation + "%, " + valueLightness + "%)";
  document.getElementById('colorDisplay').setAttribute('style', "background-color:" + color);
} 
</script>


<div>
Hue:
<br>
<input id="sliderHue" type="range" min="0" max="360" step="1" onchange="updateTextInput();">
<br>
<input type="text" id="textHue" value="">
<br>
<br>
<br>Saturation:
<br>
<input id="sliderSaturation" type="range" min="0" max="100" step="1" onchange="updateTextInput();">
<br>
<input type="text" id="textSaturation" value="">
<br>
<br>
<br>Lightness:
<br>
<input id="sliderLightness" type="range" min="0" max="100" step="1" onchange="updateTextInput();">
<br>
<input type="text" id="textLightness" value="">
</div>

That's probably because of the way you ordered it. If you put it before the elements that you are referencing, it won't work. There are a few solutions, the simplest is probably moving the script to the end of the document and the cleaner way is to download jQuery and wrap your code in a.

$(function(){ /* Code goes here */ }

here is html page as follows

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="ds.js"></script>
</head>

<body>


<div>
Hue:
<br>

<input id="sliderHue" type="range" min="0" max="360" step="1" onchange="updateTextInput();">
<br>
<input type="text" id="textHue" value="">

<br>
<br>
<br>Saturation:
<br>
<input id="sliderSaturation" type="range" min="0" max="100" step="1" onchange="updateTextInput();">
<br>
<input type="text" id="textSaturation" value="">
<br>
<br>
<br>Lightness:
<br>
<input id="sliderLightness" type="range" min="0" max="100" step="1" onchange="updateTextInput();">
<br>
<input type="text" id="textLightness" value="">
</div>

</body>
</html>

and ds.js

function updateTextInput() {
  var valueHue = document.getElementById('sliderHue').value;
  var valueSaturation = document.getElementById('sliderSaturation').value;
  var valueLightness = document.getElementById('sliderLightness').value;

  document.getElementById('textHue').value = valueHue;
  document.getElementById('textSaturation').value = valueSaturation + "%";
  document.getElementById('textLightness').value = valueLightness + "%";

  var color = "hsl(" + valueHue + ", " + valueSaturation + "%, " + valueLightness + "%)";
  document.getElementById('colorDisplay').setAttribute('style', "background-color:" + color);
} 

you will get your output

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