首页 > 解决方案 > 如何制作 JS 多按钮?

问题描述

我有名为 1,2 和 3 的按钮。下面是 a、b、c、d 按钮。当我单击第一个按钮并单击“A”按钮时,我收到 (Test Test2) 警告。但我无法单击第二个按钮并单击“A”按钮。我想再发出一个警告。例如:我单击第一个按钮,然后单击 A 按钮,如屏幕上显示的 (Test Test6),但我不能。谢谢。

var firstButtonPressed = false;
function onFirstButtonClick() {
  firstButtonPressed = true;
}

function onfourButtonClick() {
  if(firstButtonPressed) {
    alert('Test Test2');
    firstButtonPressed = false;
  } else {
    alert('Test2');
  }
  return;
}


var secondButtonPressed = false;
function onSecondButtonClick(){
  secondButtonPressed = true;
}

function onFourButtonClick(){
  if(secondButtonPressed){
    alert('Button2 Test');
    secondButtonPressed = false;
  }else {
    alert ('61 Tes-t')
  }
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button type="button" onclick="onFirstButtonClick()">1<br><br>Button</button>
  <button type="button" onclick="onSecondButtonClick()">2<br><br>Button</button>
  <button type="button" onclick="onThirdButtonClick()">3<br><br>Button</button>
  <br /><br>
  <button type="button" onclick="onfourButtonClick()">A</button>
  <button type="button" onclick="onfiveButtonClick()">B</button>
  <button type="button" onclick="onsixButtonClick()">C</button>
  <button type="button" onclick="onsevenButtonClick()">D</button>

</body>
</html>

标签: javascript

解决方案


推荐阅读