我有一个div,点击它会显示一些元素.

我遇到的问题是,这些元素与父容器重叠,并覆盖了下面的"确认"按钮.

  1. 我想让容器根据里面的元素进行调整
  2. 我如何才能使DropDown div中的元素适合它们的容器?

function mostrarMapa() {
  var mapaContainer = document.getElementById("mapa");
  var notaInput = document.getElementById("nota");
  var checkbox = document.getElementById("mostrarMapaCheckbox");

  if (checkbox.checked) {
    mapaContainer.style.display = "block";
    notaInput.style.display = "block";
  } else {
    mapaContainer.style.display = "none";
    notaInput.style.display = "none";
  }
}

function seleccionarDireccion(elemento, direccion, ciudad) {

  var elementosDireccion = document.querySelectorAll('.direcciones-barracuda');
  elementosDireccion.forEach(function(elem) {
    elem.classList.remove('selected');
  });

  elemento.classList.add('selected');

  alert('Dirección seleccionada:\n' + direccion + '\n' + ciudad);
}

function confirmar() {
  alert("Pedido confirmado");
}
.direcciones-barracuda {
  display: flex;
  align-items: center;
  margin-right: 10px;
  margin-bottom: 10px;
  cursor: pointer;
  border: 1px solid #ccc;
  border-radius: 10px;
  padding: 10px;
  transition: border-color 0.3s;
  position: relative;
}

.icono-ubicacion {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}

.direccion-info {
  display: flex;
  flex-direction: column;
  margin-left: 5px;
}

.direccion-info span {
  display: block;
}

.direcciones-barracuda.selected {
  border-color: #E05228;
}

.mapa-container {
  display: none;
  padding: 10px;
}

#mostrarMapaContainer {
  clear: both;
  border: 1px solid #ccc;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
}

#mapa {
  height: 300px;
  width: 100%;
  margin-top: 10px;
}

#nota {
  width: 100%;
  margin-top: 10px;
}

#confirmarBtn {
  display: block;
  margin: 20px auto;
  padding: 10px 20px;
  background-color: #E05228;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
<div class="card" style="padding: 1rem;">
  <div class="table-wrapper">
    <span>Where do we send your order?</span>

    <div style="display: flex; flex-wrap: wrap;">

      <div class="direcciones-barracuda">
        <span>SOME TEXT</span>
      </div>

    </div>
    <div id="mostrarMapaContainer">
      <input type="checkbox" id="mostrarMapaCheckbox" onclick="mostrarMapa()">
      <label for="mostrarMapaCheckbox">New Adress</label>
      <div id="mapa" class="mapa-container">
        <input type="text" id="direccionCompleta" placeholder="Enter full address" style="width: 100%; box-sizing: border-box; padding: 10px; margin-bottom: 10px;">
        <!-- Agregado un mapa ficticio para mostrar la dirección seleccionada -->
        <div style="height: 200px; background-color: #f0f0f0; margin-bottom: 10px;"></div>

        <input type="text" id="referencia" placeholder="References" style="width: 100%; box-sizing: border-box; padding: 10px; margin-bottom: 10px;">

        <div style="display: flex; justify-content: space-between;">
          <input type="text" id="nota" placeholder="Notes" style="width: 48%; box-sizing: border-box; padding: 10px;">
          <input type="text" id="celular" placeholder="Phone" style="width: 48%; box-sizing: border-box; padding: 10px;">
        </div>
      </div>
    </div>

    <button class="" id="confirmarBtn" onclick="confirmar()">CONFIRM</button>
  </div>
</div>

推荐答案

删除#mapa元素上的height设置.你是在强迫它达到一个特定的高度,即使它的内容更长.如果删除该设置,元素将动态调整自身大小.

#mapa {
  /* height: 300px; REMOVE THIS */
  width: 100%;
  margin-top: 10px;
}

还要注意,您可以通过从您的HTML代码中删除内联的CSS样式和JS事件处理程序来提高代码的质量.将CSS放在外部样式表中(因为您已经有一个样式表,所以没有理由不使用它),并将事件处理程序直接绑定到JS代码中.

下面是一个实现了上述修复的工作示例:

document.querySelector('#mostrarMapaCheckbox').addEventListener('click', () => {
  var mapaContainer = document.getElementById("mapa");
  var notaInput = document.getElementById("nota");
  var checkbox = document.getElementById("mostrarMapaCheckbox");

  if (checkbox.checked) {
    mapaContainer.style.display = "block";
    notaInput.style.display = "block";
  } else {
    mapaContainer.style.display = "none";
    notaInput.style.display = "none";
  }
})

function seleccionarDireccion(elemento, direccion, ciudad) {
  var elementosDireccion = document.querySelectorAll('.direcciones-barracuda');
  elementosDireccion.forEach(function(elem) {
    elem.classList.remove('selected');
  });

  elemento.classList.add('selected');
  alert('Dirección seleccionada:\n' + direccion + '\n' + ciudad);
}

document.querySelector('#confirmarBtn', () => {
  alert("Pedido confirmado");
});
.direcciones-barracuda-wrapper {
  display: flex;
  flex-wrap: wrap;
}

.direcciones-barracuda {
  display: flex;
  align-items: center;
  margin-right: 10px;
  margin-bottom: 10px;
  cursor: pointer;
  border: 1px solid #ccc;
  border-radius: 10px;
  padding: 10px;
  transition: border-color 0.3s;
  position: relative;
  display: flex;
  flex-wrap: wrap;
}

.icono-ubicacion {
  width: 20px;
  height: 20px;
  margin-right: 5px;
}

.direccion-info {
  display: flex;
  flex-direction: column;
  margin-left: 5px;
}

.direccion-info span {
  display: block;
}

.direcciones-barracuda.selected {
  border-color: #E05228;
}

.mapa-container {
  display: none;
  padding: 10px;
}

#mostrarMapaContainer {
  clear: both;
  border: 1px solid #ccc;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
}

#direccionCompleta,
#referencia {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-bottom: 10px;
}

#mapa {
  width: 100%;
  margin-top: 10px;
}

.nota-wrapper {
  display: flex;
  justify-content: space-between;
}

#nota {
  margin-top: 10px;
  width: 48%;
  box-sizing: border-box;
  padding: 10px;
}

#celular {
  width: 48%;
  box-sizing: border-box;
  padding: 10px;
}

#confirmarBtn {
  display: block;
  margin: 20px auto;
  padding: 10px 20px;
  background-color: #E05228;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.foo {
  height: 200px;
  background-color: #f0f0f0;
  margin-bottom: 10px;
}
<div class="card" style="padding: 1rem;">
  <div class="table-wrapper">
    <span>Where do we send your order?</span>
    <div class="direcciones-barracuda-wrapper">
      <div class="direcciones-barracuda">
        <span>SOME TEXT</span>
      </div>
    </div>
    <div id="mostrarMapaContainer">
      <input type="checkbox" id="mostrarMapaCheckbox" />
      <label for="mostrarMapaCheckbox">New Adress</label>
      <div id="mapa" class="mapa-container">
        <input type="text" id="direccionCompleta" placeholder="Enter full address" />
        <div class="foo"></div>
        <input type="text" id="referencia" placeholder="References" />
        <div class="nota-wrapper">
          <input type="text" id="nota" placeholder="Notes" />
          <input type="text" id="celular" placeholder="Phone" />
        </div>
      </div>
    </div>
    <button class="" id="confirmarBtn">CONFIRM</button>
  </div>
</div>

Javascript相关问答推荐

如何访问react路由v6加载器函数中的查询参数/搜索参数

React Native平面列表自动滚动

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

如何使用Echart 5.5.0创建箱形图

使搜索栏更改语言

康威的生活游戏规则在我的Javascript版本中不起作用.''我做错了什么?

v—自动完成不显示 Select 列表中的所有项目

如何在JavaScript文件中使用Json文件

如何解决useState错误—setSelect Image不是函数''

未加载css colored颜色 ,无法将div设置为可见和不可见

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

在FAQ Accodion应用程序中使用React useState时出现问题

有效路径同时显示有效路径组件和不存在的路径组件

Socket.IO在刷新页面时执行函数两次

按特定顺序将4个数组组合在一起,按ID分组

在单击按钮时生成多个表单时的处理状态

TypeORM QueryBuilder限制联接到一条记录

是否有静态版本的`instanceof`?

不允许在对象文本中注释掉的属性

元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但GET:Object.在Reaction项目中