如何让<div>中的两个元素调用不同的函数?

How to get two elements in a <div> to call different functions?

我是 javascript/html/css 的新手,如果这看起来像是一个简单的问题,请原谅,但我正在页面上构建一个叠加层,并且有两个元素具有 onclick 函数。

<div id="calibration" class="overlay">

  <a href="#" class="closeBtn" onclick="closeCalibration()">&times;</a>
  <a href="#" class="calDot" onclick="moveCalibrationDot()">&bull;</a>

  <div class="overlay__content">
    <a href="#">Please look at the red dots and click on them as they appear.</a>
  </div>

</div>

然而,这两个元素似乎调用了相同的函数,而不是它们应该调用的两个不同的函数。这两个函数是,

document.getElementById('calibration').style.height = "100%";


function closeCalibration() {
  document.getElementById('calibration').style.height = "0%";
}

function moveCalibrationDot() {
  console.log("Hello");
}

如果有帮助,这里是一个 link 的 jsfiddle,https://jsfiddle.net/mnc8m3tr/2/

我使用的 css 是

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 0%;

background: rgba(1, 1, 1, .9);
  overflow-x: hidden;
  transition: all 0.5s;
  z-index: 1;
}

.overlay__content {
  position: relative;
  top: 25%;
  width: 100%;
  text-align: center;
  margin-top: 30px;
}

.overlay a {
  padding: 10px;
  color: #fff;
  font-size: 30px;
  text-decoration: none;
  display: block;
}

.overlay .closeBtn {
  position: absolute;
  top: 20px;
  right: 50px;
  font-size: 50px;
}

.overlay .calDot {
  position: relative;
  top: 5%;
  left: 5%;
  color: #f00;
  font-size: 50px;
}

打开您的 jsfiddle,然后在 chrome 中打开 DevTools(按 F12)。 然后检查您的按钮,您会发现其中一个链接非常宽并且覆盖了关闭按钮。

这个点出现在 "x" link 上方是因为它的定位,所以只要你点击 "x",你就在点击这个点。制作点 position: absolute; 并且它不会与另一个按钮重叠。

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 0%;
  background: rgba(1, 1, 1, .9);
  overflow-x: hidden;
  transition: all 0.5s;
  z-index: 1;
}

.overlay__content {
  position: relative;
  top: 25%;
  width: 100%;
  text-align: center;
  margin-top: 30px;
}

.overlay a {
  padding: 10px;
  color: #fff;
  font-size: 30px;
  text-decoration: none;
  display: block;
}

.overlay .closeBtn {
  position: absolute;
  top: 20px;
  right: 50px;
  font-size: 50px;
}

.overlay .calDot {
  position: absolute;
  top: 5%;
  left: 5%;
  color: #f00;
  font-size: 50px;
}
<div id="calibration" class="overlay">

  <a href="#" class="closeBtn" onclick="closeCalibration()">&times;</a>
  <a href="#" class="calDot" onclick="moveCalibrationDot()">&bull;</a>

  <div class="overlay__content">
    <a href="#">Please look at the red dots and click on them as they appear.</a>
  </div>

</div>

<script>
  document.getElementById('calibration').style.height = "100%";


  function closeCalibration() {
    document.getElementById('calibration').style.height = "0%";
  }

  function moveCalibrationDot() {
    console.log("Hello");
  }

</script>