【问题标题】:Can't click on other tabs in html tabcontent无法单击 html tabcontent 中的其他选项卡
【发布时间】:2020-02-25 03:09:00
【问题描述】:

每当我在我的新个人资料选项卡上时,我都无法单击其他选项卡转到它们。我在下面包含了我的代码。我可以从我的仪表板选项卡转到新配置文件选项卡,但反过来不行。我也可以从新任务转到新配置文件,唯一的例外是当我从新配置文件转到另一个选项卡时。​​p>

document.getElementById("defaultOpen").click();

function openCity(evt, cityName) {
  // Declare all variables
  var i, tabcontent, tablinks;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // Get all elements with class="tablinks" and remove the class "active"
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the button that opened the tab
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
  console.log("hello");
}
label {
  display: inline-block;
  width: 140px;
  text-align: right;
}

input {
  width: 140px
}

select {
  width: 140px;
}


/* Split the screen in half */

.split {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 1;
  top: 0;
  overflow-x: hidden;
  padding-top: 20px;
}


/* If you want the content centered horizontally and vertically */

.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}


/* Control the left side */

.left {
  left: 0;
  /*background-color: #111; */
}


/* Control the right side */

.right {
  right: 0;
  /*background-color: red; */
}


/* Style the tab */

.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
  position: relative;
  clear: both;
}


/* Style the buttons that are used to open the tab content */

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
}


/* Change background color of buttons on hover */

.tab button:hover {
  background-color: #ddd;
}


/* Create an active/current tablink class */

.tab button.active {
  background-color: #ccc;
}


/* Style the tab content */

.tabcontent {
  display: none;
  padding: 1280px 720px;
  border: 1px solid #ccc;
  border-top: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Document</title>


</head>

<body>
  <!-- For the tab headers-->
  <div class="tab">
    <button class="tablinks" onclick="openCity(event, 'ProfileTab')">New Profile</button>
    <button class="tablinks" onclick="openCity(event, 'TaskTab')" id="defaultOpen">New Task</button>
    <button class="tablinks" onclick="openCity(event, 'DashboardTab')">Dashboard</button>
  </div>


  <div id="ProfileTab" class="tabcontent">
    <!-- Shipping and billing details will be on left half of screen. -->
    <div class="split left">
      <!-- Shipping Details -->
      <h1>Shipping Details</h1>
      <div class="block">
        <label for="firstNameTextFieldShip">First Name:</label>
        <input type="text" id="firstNameTextFieldShip" name="firstNameShip">


        <label for="lastNameTextFieldShip">Last Name:</label>
        <input type="text" id="lastNameTextFieldShip" name="lastNameShip">
      </div>

      <br>

      <div class="block">
        <label for="Address1TextFieldShip">Address:</label>
        <input type="text" id="Address1TextFieldShip" name="add1txtfieldShip">

        <label for="Address2TextField">Address 2:</label>
        <input type="text" id="Address2TextFieldShip" name="add2txtfieldShip">
      </div>

      <br>

      <div class="block">
        <label for="ZipTextFieldShip">Zip Code:</label>
        <input type="text" id="ZipTextFieldShip" name="ziptxtfieldShip">

        <label for="CityTextFieldShip">City:</label>
        <input type="text" id="CityTextFieldShip" name="citytxtfieldShip">
      </div>

      <br>

      <div class="block">
        <label for="CountriesSelectShip">Country:</label>
        <select id="CountriesSelectShip">
          <option value="USA">USA</option>
        </select>

        <label for="StateTextFieldShip">State:</label>
        <input type="text" id="StateTextFieldShip" name="statetxtfieldShip">
      </div>



      <!-- Billing Details-->
      <h1>Billing Details</h1>
      <div class="block">
        <label for="firstNameTextFieldBilling">First Name:</label>
        <input type="text" id="firstNameTextFieldBilling" name="firstNameBilling">


        <label for="lastNameTextFieldBilling">Last Name:</label>
        <input type="text" id="lastNameTextFieldBilling" name="lastNameBilling">
      </div>

      <br>

      <div class="block">
        <label for="Address1TextFieldBilling">Address:</label>
        <input type="text" id="Address1TextFieldBilling" name="add1txtfieldBilling">

        <label for="Address2TextFieldBilling">Address 2:</label>
        <input type="text" id="Address2TextFieldBilling" name="add2txtfieldBilling">
      </div>

      <br>

      <div class="block">
        <label for="ZipTextFieldBilling">Zip Code:</label>
        <input type="text" id="ZipTextFieldBilling" name="ziptxtfieldBilling">

        <label for="CityTextFieldBilling">City:</label>
        <input type="text" id="CityTextFieldBilling" name="citytxtfieldBilling">
      </div>

      <br>

      <div class="block">
        <label for="CountriesSelectBilling">Country:</label>
        <select id="CountriesSelectBilling">
          <option value="USA">USA</option>
        </select>

        <label for="StateTextFieldBilling">State:</label>
        <input type="text" id="StateTextFieldBilling" name="statetxtfieldBilling">
      </div>
    </div>

    <div class="split right">

      <h1>Payment Information</h1>
      <div class="block">
        <label for="ProfileNameTextField">Profile Name:</label>
        <input type="text" id="ProfileNameTextField" name="profileNameTextField">
      </div>

      <br>

      <div class="block">
        <label for="EmailTextField">Email:</label>
        <input type="text" id="EmailTextField" name="emailTextField">
      </div>

      <br>

      <div class="block">
        <label for="PhoneNumTextField">Phone Number:</label>
        <input type="text" id="PhoneNumTextField" name="PhoneNumTextField">
      </div>

      <br>

      <div class="block">
        <label for="NameOnCardTextField">Name On Card:</label>
        <input type="text" id="NameOnCardTextField" name="NameOnCardTextField">
      </div>

      <br>

      <div class="block">
        <label for="NumOnCardTextField">Number On Card:</label>
        <input type="text" id="NumOnCardTextField" name="NumOnCardTextField">
      </div>

      <br>

      <div class="block">
        <label for="ExpirationTextField">Expiration Date:</label>
        <input type="text" id="ExpirationTextField" name="ExpirationTextField">
      </div>

      <br>

      <div class="block">
        <label for="CVVTextField">CVV:</label>
        <input type="text" id="CVVTextField" name="CVVTextField">
      </div>

      <br>
    </div>

  </div>

  <!-- Tasks tab -->
  <div id="TaskTab" class="tabcontent">
    <h1>hi</h1>
  </div>

  <!-- Dashboard tab -->
  <div id="DashboardTab" class="tabcontent">

  </div>


</body>

</html>

Can't click on dashboard or New Task tab after being on New Profile tab

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    您的标签按预期工作。您的容器“div id="ProfileTab" class="tabcontent" style="display: block;" 位于标签顶部,因此您无法单击它们。您只需要修复该容器的位置,这样它没有阻止标签

    【讨论】:

      【解决方案2】:

      快速回答:删除.tabcontent 上的填充并删除.split 上的位置。然后将float: left; 添加到.split

      总体问题是tabcontent 的内容超出了文档流,导致它与 DOM 中的选项卡重叠。因此,即使您看起来像是在单击选项卡,但实际上您是在单击 tabcontent

      浮动是一种快速修复并排显示事物的方法,并不是真正的“错误”。现在的首选是使用 flexbox。我建议查一下。这是一个指南和一个有趣的游戏: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

      https://flexboxfroggy.com/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多