为什么 document.getElementById 不能在函数中工作

问题描述 投票:0回答:1

`为什么它在函数之外工作?怎么了?我不明白......尝试添加返回语句 - 仍然没有。 [[enter image description here](https://i.stack.imgur.com/ZWAvj.png)](https://i.stack.imgur.com/HteFI.png)

不起作用:

function changeValue() {
    let charge = document.getElementById("myRange").value;
document.getElementById("displayValue").innerHTML = charge;

}

作品:

 let charge = document.getElementById("myRange").value;
document.getElementById("displayValue").innerHTML = charge;

我的 HTML:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Battery Emulatior</title>
  <link rel="icon" type="image/x-icon" href="favicon.png">
  <link rel="stylesheet" href="battery.css">
  
</head>

<body>

  <h1>INTERACTIVE BATTERY EMULATOR</h1>
  <p1>Choose the expiration period</p1>
  <div class="indicator"> 
    <div class="batteryterminal"></div>
  </div>
  <div class="slidecontainer">
    <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
  </div>
  <div id="displayValue"></div>
  
  <script src="battery.js"></script>
</body>

</html>

` 尝试添加返回语句 - 什么都没有

javascript html web dom
1个回答
0
投票

您需要调用该函数才能执行

function changeValue() {
  let charge = document.getElementById("myRange").value;
  document.getElementById("displayValue").innerHTML = charge;
}
changeValue()
© www.soinside.com 2019 - 2024. All rights reserved.