如何在两个按钮之间切换

dm7nw8vv  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(342)

这是密码:https://jsfiddle.net/magicschoolbusdropout/dwbmo3aj/18/
目前,我的代码允许我点击一个按钮,内容打开,点击内容关闭的同一个按钮。然后我点击第二个按钮,内容打开,点击同一个按钮内容关闭。我可以从第二个按钮切换到第一个按钮,但随后会追加内容。
我想做的是:在两个按钮之间切换。单击第一个按钮,然后单击第二个按钮,信息不会附加或移动。非常感谢您的帮助!!谢谢:)
以下是我尝试在以下功能之间切换的功能:

function currentOrder(){
  var current = document.getElementById("current");
  if (current.style.display === "none") {
    current.style.display = "block";  
  } else {
    current.style.display = "none";
  }
}

function orderHistory(){
  var history = document.getElementById("history");
  if (history.style.display === "none") {
    history.style.display = "block";
  } else {
    history.style.display = "none";
  }
}

<<html>

<head>
  <script src="https://static.freshdev.io/fdk/2.0/assets/fresh_client.js"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"
    integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <link rel="stylesheet" type="text/css" href="https://static.freshdev.io/fdk/2.0/assets/freshdesk.css" />
</head>

<body>
<button onclick="currentOrder()">Current Order</button>
<button onclick="orderHistory()">Order History</button>

<div id ="current">
  <div class="">
    <div class="item1-container">
      <div class="container">
        <br>
        <h5 class="text"><span class="key"> Order #: </span><span class="value">121800</span></h5>
        <h5 class="text line"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
        <h5 class="text"><span class="key"> Status Update : </span><span class="value">Pending</span></h5>
        <br>
        <h4 class="text"><span class="key"> <b> Non-Dairy Blue Matcha </b></span></h4>
        <img src="matchadrink.png" ALIGN=left alt="item1 image" class="image">
        <div class="container_text" id="item1_details">
          <p class="text"><span class="key">Price: </span><span class="value">$</span><span class="value"
              id="item-price" style="margin-left:auto">4.44</span></p>
          <p class="text"><span class="key">Quantity: </span><span id="quantity" class="value">0</span></p>
          <div class="quantity-wrapper">
            <div class="value-button" id="increase" onclick="increaseValue()" value="Increase Value"></div>
            <input type="number" id="number" value="0" />
            <div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value"></div>
            <input type="submit" name="save" value="save" id="add_quantity">
          </div>
        </div>
      </div>
    </div>
  </div><br>
  <div class="">
    <div class="item2-container">
      <div class="container">
        <h4 class="text"><span class="key"> <b>S'mores FroYo</b></span></h4>
        <img src="smores.png" ALIGN=left alt="item2 image" class="image">
        <div class="container_text" id="item1_details">
          <p class="text"><span class="key">Price: </span><span class="value">$</span><span class="value"
              id="item-price">5.20</span></p>
          <p class="text"><span class="key">Quantity: </span><span id="quantity1" class="value">0</span></p>
          <div class="quantity-wrapper">
            <div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value"></div>
            <input type="number1" id="number1" value="0" />
            <div class="value-button" id="increase" onclick="increaseValue()" value="Increase Value"></div>
            <input type="submit" name="save" value="save" id="add_quantity1">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="history">
  <div class="">
 <h3> Order History</h3> <br>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">123456</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">56789</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">10987</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">082402</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
 <p>
  <h5 class="text"><span class="key"> Order #: </span><span class="value">081893</span></h5>
  <h5 class="text"><span class="key"> Name: </span><span id="name" class="value"></span></h5>
  <h5 class="text line"><span class="key"> Status Update : </span><span class="value">Completed</span></h5>
 </p>
  </div>
</div>
</body>
<script src="app.js"></script>

</html>>
ijxebb2r

ijxebb2r1#

只需将赋值移到函数之外。
这应该起作用:

let current = document.getElementById("current");
let history = document.getElementById("history");

function currentOrder(){
    current.style.display = "block";
    history.style.display = "none"
}

function orderHistory(){
    current.style.display = "none";
    history.style.display = "block"
}

您不需要检查当前状态,只需切换样式即可。保持简单(吻)。
干杯

相关问题