元素用于包装链接列表项,
元素用于创建链接,并添加了.list-group-item和.list-group-item-action类。当鼠标悬停在链接上时,.list-group-item-action类将添加灰色背景色。
运行结果
移除列表边框
在Bootstrap 5中,.list-group-flush类可以用于移除列表的边框和圆角。这个类可以应用于.list-group元素,以创建一个没有边框和圆角的列表组。
实例
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<div class="list-group list-group-flush">
<a href="#" class="list-group-item list-group-item-action">项目 1a>
<a href="#" class="list-group-item list-group-item-action">项目 2a>
<a href="#" class="list-group-item list-group-item-action">项目 3a>
运行结果
带编号的列表组
在Bootstrap 5中,可以使用 .list-group-numbered 类创建前面带有数字的列表项。
实例
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<ol class="list-group list-group-numbered">
<li class="list-group-item">第一项li>
<li class="list-group-item">第二项li>
<li class="list-group-item">第三项li>
<li class="list-group-item">第四项li>
运行结果
水平列表组
在Bootstrap 5中,可以将.list-group-horizontal类添加到 .list-group类中,来创建一个水平列表组,其中列表项水平显示而不是垂直显示。
实例
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<ol class="list-group list-group-horizontal">
<li class="list-group-item">第一项li>
<li class="list-group-item">第二项li>
<li class="list-group-item">第三项li>
<li class="list-group-item">第四项li>
在这个示例中,.list-group-horizontal类被添加到.list-group元素上,以使列表项水平显示。
运行结果
多种颜色列表项
在Bootstrap中,可以使用不同的类来设置列表项的颜色。以下是一些可用的类:
- .list-group-item-success:设置成功(绿色)背景色。
- .list-group-item-secondary:设置次要(灰色)背景色。
- .list-group-item-info:设置信息(蓝色)背景色。
- .list-group-item-warning:设置警告(黄色)背景色。
- .list-group-item-danger:设置危险(红色)背景色。
- .list-group-item-dark:设置深色(黑色)背景色。
- .list-group-item-light:设置浅色(白色)背景色。
这些类可以应用于.list-group-item元素,以改变其背景颜色。可以在一个列表项上使用多个颜色类,但请注意,Bootstrap的颜色类会覆盖彼此,因此最后应用的类将决定最终的颜色。
实例
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<li class="list-group-item">初始项目li>
<li class="list-group-item list-group-item-success">成功项目li>
<li class="list-group-item list-group-item-secondary">次要项目li>
<li class="list-group-item list-group-item-info">信息项目li>
<li class="list-group-item list-group-item-warning">警告项目li>
<li class="list-group-item list-group-item-danger">危险项目li>
<li class="list-group-item list-group-item-dark">深色项目li>
<li class="list-group-item list-group-item-light">浅色项目li>
运行结果
多种颜色的链接列表项
实例
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<a href="#" class="list-group-item list-group-item-action">激活列表项a>
<a href="#" class="list-group-item list-group-item-success">成功列表项a>
<a href="#" class="list-group-item list-group-item-secondary">次要列表项a>
<a href="#" class="list-group-item list-group-item-info">信息列表项a>
<a href="#" class="list-group-item list-group-item-warning">警告列表项a>
<a href="#" class="list-group-item list-group-item-danger">危险列表项a>
<a href="#" class="list-group-item list-group-item-primary">主要列表项a>
<a href="#" class="list-group-item list-group-item-dark">深灰色列表项a>
<a href="#" class="list-group-item list-group-item-light">浅色列表项a>
运行结果
带徽章的列表组
在Bootstrap 5中,可以使用.badge类来创建一个徽章,并将其与列表项结合使用。徽章通常用于显示一些额外的信息或标记,例如提示、警告或状态。
实例
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<li class="list-group-item d-flex justify-content-between align-items-center">
<span class="badge bg-primary rounded-pill">主要span>
<li class="list-group-item d-flex justify-content-between align-items-center">
<span class="badge bg-secondary rounded-pill">次要span>
运行结果
列表组案例
实例一
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<li class="list-group-item d-flex justify-content-between align-items-start border-0">
<button class="btn btn-primary">编辑button>
<button class="btn btn-secondary">删除button>
<li class="list-group-item d-flex justify-content-between align-items-start border-0">
<button class="btn btn-primary">编辑button>
<button class="btn btn-secondary">删除button>
<li class="list-group-item d-flex justify-content-between align-items-start border-0">
<button class="btn btn-primary">编辑button>
<button class="btn btn-secondary">删除button>
<li class="list-group-item d-flex justify-content-between align-items-start border-0">
<button class="btn btn-primary">编辑button>
<button class="btn btn-secondary">删除button>
<li class="list-group bg-dark text-white p-0">最后的项目(背景色)li>
window.onload = function() {
var editButtons = document.querySelectorAll(".btn-primary");
var deleteButtons = document.querySelectorAll(".btn-secondary");
var pElements = document.querySelectorAll("p");
editButtons.forEach(function(button, index) {
button.addEventListener("click", function() {
var newDescription = prompt("请输入新的项目描述:");
pElements[index].innerText = newDescription;
deleteButtons.forEach(function(button, index) {
button.addEventListener("click", function() {
var confirmDelete = confirm("确定要删除此项目吗?");
var listItem = this.parentNode.parentNode;
运行结果
实例二
<title>Bootstrap 实例title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js">script>
<div class="container mt-3">
<li class="list-group-item">
<button class="btn btn-primary btn-sm add-nested-item">添加嵌套列表项button>
<button class="btn btn-primary btn-sm edit-nested-item">更改嵌套列表项button>
<button class="btn btn-secondary btn-sm delete-nested-item">删除button>
<li class="list-group-item">嵌套列表项1li>
<li class="list-group-item">嵌套列表项2li>
<li class="list-group-item">嵌套列表项3li>
<li class="list-group-item">
<button class="btn btn-primary btn-sm add-nested-item">添加嵌套列表项button>
<button class="btn btn-primary btn-sm edit-nested-item">更改嵌套列表项button>
<button class="btn btn-secondary btn-sm delete-nested-item">删除button>
<li class="list-group-item">嵌套列表项4li>
<li class="list-group-item">嵌套列表项5li>
<li class="list-group-item">嵌套列表项6li>
<li class="list-group-item">
<button class="btn btn-primary btn-sm add-nested-item">添加嵌套列表项button>
<button class="btn btn-primary btn-sm edit-nested-item">更改嵌套列表项button>
<button class="btn btn-secondary btn-sm delete-nested-item">删除button>
<li class="list-group-item">嵌套列表项7li>
<li class="list-group-item">嵌套列表项8li>
<li class="list-group-item">嵌套列表项9li>
window.onload = function() {
var addButtons = document.querySelectorAll(".add-nested-item");
var deleteButtons = document.querySelectorAll(".delete-nested-item");
var editButtons = document.querySelectorAll(".edit-nested-item");
addButtons.forEach(function(button) {
button.addEventListener("click", function() {
var newNestedItem = document.createElement("li");
newNestedItem.className = "list-group-item";
newNestedItem.innerText = "新的嵌套列表项";
var parentList = button.parentNode.querySelector("ul.list-group");
parentList.appendChild(newNestedItem);
editButtons.forEach(function(button) {
button.addEventListener("click", function() {
var parentList = button.parentNode.querySelector("ul.list-group");
var nestedItems = parentList.querySelectorAll("li.list-group-item");
nestedItems.forEach(function(nestedItem) {
nestedItem.innerText = "更改后的嵌套列表项";
deleteButtons.forEach(function(button) {
button.addEventListener("click", function() {
var nestedItem = button.parentNode;
运行结果