如何使用.innerHTML与let创建的var

时间:2018-06-11 08:17:29

标签: javascript

我试图在javascript中创建一个非常简单的应用程序,以便将rgb颜色除以255.我希望结果显示在index.html页面上,但它没有工作可以有人告诉我如何解决它。

这是我的HTML代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>app</title>
    </head>
    <body>
        <script src="app.js"></script>
        <p id="red"></p>
    </body>
</html>

这是我的javascript代码:

let red = prompt("Enter the red number here");
let green = prompt("Enter the green number here");
let blue = prompt("Enter the blue number here.");

let resultR = red/255; 
let resultG = green/255;
let resultB = blue/255;

document.getElementById("red").innerHTML = resultR;

1 个答案:

答案 0 :(得分:-2)

虽然工作正常......

问题可能是您在Html之前调用JavaScript,因此无法找到该元素。

let red = prompt("Enter the red number here");
let green = prompt("Enter the green number here");
let blue = prompt("Enter the blue number here.");

let resultR = red/255; 
let resultG = green/255;
let resultB = blue/255;

document.getElementById("red").innerHTML = resultR;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>app</title>
</head>
<body>
<script src="app.js"></script>
<p id="red"></p>
</body>
</html>