Pilih2 Pilih menu lungsur turun: Kosongkan ikon yang bertindih dengan teks
P粉156415696
P粉156415696 2024-02-25 14:54:04
0
2
488

Saya menghadapi isu di mana teks bertindih di bawah butang "Kosongkan" X semasa tuding dalam menu pilihan. Menambah padding di sebelah kanan bukanlah penyelesaian kerana ia menolak ikon di luar kotak. Saya menggunakan perpustakaan Select2 JS yang terdapat di sini https://select2.org/ dan Bootstrap 5

$("#search_bar").select2({
  theme: 'bootstrap-5',
  placeholder: "Some text",
  allowClear: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js" integrity="sha512-2ImtlRlf2VVmiGZsjm9bEyhjGW4dU7B6TNwh/hx/iSByxNENtj3WVE6o/9Lj4TJeVXPi4bnOIMXFIJJAeufa0A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" integrity="sha512-nMNlpuaDPrqlEls3IX/Q56H36qvBASwb3ipuo3MxeWbsQB1881ox0cRv7UPTgBlriqoynt35KjEwgGUeUXIPnw==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/select2-bootstrap-5-theme.min.css" />

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


<div class="">
  <select id="search_bar" class="form-select w-25">
    <option value="1">
      This is a long option string
    </option>
    <option value="2">
      This is another long option string
    </option>
  </select>
</div>

P粉156415696
P粉156415696

membalas semua(2)
P粉155832941

Tambah CSS berikut.

.select2-selection__clear {
    position: absolute !important;
    right: 2px !important;
}

https://jsfiddle.net/8n23bd6q/

P粉345302753

Ikuti dokumentasi di sini Pilih2 Dokumentasi. Anda boleh menggunakan width: 'style' 和标签 <select> 中的内联样式来扩展 select2 宽度。这里我将select的宽度设置为200px

Kemudian, dengan melihat struktur DOM select2 (menggunakan Chrome Devtool), saya mendapati bahawa kita boleh membuat butang X tidak bertindih dengan teks dengan menambah CSS berikut:

.select2-selection.select2-selection--single {
  padding-right: 50px !important;
}

$("#search_bar").select2({
  theme: 'bootstrap-5',
  placeholder: "Some text",
  allowClear: true,
  width: 'style'
});
.select2-selection.select2-selection--single {
  padding-right: 50px !important;
}
sssccc
sssccc

[email protected]/dist/select2-bootstrap-5-theme.min.css" />

[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan