【问题标题】:How to overlap a html div over another div?如何将 html div 重叠在另一个 div 上?
【发布时间】:2019-08-10 17:11:36
【问题描述】:

为了在 html 中创建多节下拉菜单,我使用普通选择和带有选项的 div 作为复选框。当我单击选择时,div 出现在以下元素的顶部。但我需要带有复选框的 div,因为它与以下元素重叠。这是我的代码。

代码:

var expanded = false;

function showCheckboxes() {
  var checkboxes = document.getElementById("checkboxes");
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}
<form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
      <select>
        <option>Select an option</option>
      </select>
      <div class="overSelect"></div>
    </div>
    <div id="checkboxes">
      <label for="one">
        <input type="checkbox" id="one" />First checkbox</label>
      <label for="two">
        <input type="checkbox" id="two" />Second checkbox</label>
      <label for="three">
        <input type="checkbox" id="three" />Third checkbox</label>
    </div>
    <div>
    <button type="button">Submit</button>
    </div>
  </div>
</form>

如何更改代码以满足要求?

【问题讨论】:

  • 实际上你想重叠哪个div?
  • 'checkboxes' div 应该与按钮重叠 div

标签: javascript html css checkbox html-select


【解决方案1】:

position: absolute; 用作您的#checkboxes

var expanded = false;

function showCheckboxes() {
  var checkboxes = document.getElementById("checkboxes");
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  background: #fff;
  position: absolute;
  border: 1px #dadada solid;
  width: 100%;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}
<form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
      <select>
        <option>Select an option</option>
      </select>
      <div class="overSelect"></div>
      <div id="checkboxes">
        <label for="one">
        <input type="checkbox" id="one" />First checkbox</label>
        <label for="two">
        <input type="checkbox" id="two" />Second checkbox</label>
        <label for="three">
        <input type="checkbox" id="three" />Third checkbox</label>
      </div>
    </div>
    <div>
      <button type="button">Submit</button>
    </div>
  </div>
</form>

【讨论】:

  • 像冠军一样工作!非常感谢!
猜你喜欢
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
相关资源
最近更新 更多