javascript - 如何将一个 html div 重叠在另一个 div 上?

标签 javascript html css checkbox html-select

为了在 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>

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

最佳答案

为你的#checkboxes使用position: absolute;

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>

关于javascript - 如何将一个 html div 重叠在另一个 div 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440364/

相关文章:

css - 是否有一种算法以编程方式将 TTF 字体分类为通用字体系列?

html - 图像元素在 Div 中换行

javascript - $.getJSON() 不适用于 chrome

javascript - 获取精确宽度元素

javascript - WebStorm 内置 Web 服务器为 index.html 中包含的每个 css 和 js 文件获取 404

jquery - 我的响应式菜单在移动设备上不起作用

javascript - TypeError : . val 不是函数

javascript - Windows Phone 8.1 使用 javascript 保存数据

javascript - 当滚动到元素的类为 '#wrap' 时如何更改 css?

python - 提交表单为空——Django