首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用css更改启动下拉列表激活时的背景色

使用CSS可以通过修改伪类选择器:focus来更改启动下拉列表激活时的背景色。以下是一个示例代码:

代码语言:txt
复制
<!DOCTYPE html>
<html>
<head>
<style>
/* 定义下拉列表样式 */
.custom-select {
  position: relative;
  font-family: Arial;
}

.custom-select select {
  display: none; /* 隐藏原生下拉列表 */
}

/* 定义自定义下拉列表样式 */
.select-selected {
  background-color: #ccc; /* 设置默认背景色 */
}

/* 定义下拉列表激活时的样式 */
.select-selected:focus {
  background-color: #f00; /* 设置激活时的背景色 */
}

/* 定义下拉列表箭头样式 */
.select-selected:after {
  content: "";
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid #000;
}

/* 定义下拉列表选项样式 */
.select-items div {
  cursor: pointer;
  padding: 8px 16px;
  background-color: #f9f9f9;
  color: #000;
}

/* 定义下拉列表选项悬停时的样式 */
.select-items div:hover {
  background-color: #aaa;
}

/* 定义下拉列表容器样式 */
.select-items {
  position: absolute;
  background-color: #f9f9f9;
  width: 100%;
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid #ccc;
  z-index: 999;
}

/* 定义下拉列表打开时的样式 */
.select-items.open {
  display: block;
}

</style>
</head>
<body>

<!-- 创建自定义下拉列表 -->
<div class="custom-select">
  <select>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</div>

<script>
var x, i, j, selElmnt, a, b, c;
/* 查找所有自定义下拉列表 */
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
  selElmnt = x[i].getElementsByTagName("select")[0];
  /* 创建自定义下拉列表容器 */
  a = document.createElement("DIV");
  a.setAttribute("class", "select-selected");
  a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  x[i].appendChild(a);
  /* 创建下拉列表选项容器 */
  b = document.createElement("DIV");
  b.setAttribute("class", "select-items select-hide");
  for (j = 0; j < selElmnt.length; j++) {
    c = document.createElement("DIV");
    c.innerHTML = selElmnt.options[j].innerHTML;
    c.addEventListener("click", function(e) {
        /* 当选择一个选项时,更新自定义下拉列表的显示值 */
        var y, i, k, s, h;
        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
        h = this.parentNode.previousSibling;
        for (i = 0; i < s.length; i++) {
          if (s.options[i].innerHTML == this.innerHTML) {
            s.selectedIndex = i;
            h.innerHTML = this.innerHTML;
            y = this.parentNode.getElementsByClassName("same-as-selected");
            for (k = 0; k < y.length; k++) {
              y[k].removeAttribute("class");
            }
            this.setAttribute("class", "same-as-selected");
            break;
          }
        }
        h.click();
    });
    b.appendChild(c);
  }
  x[i].appendChild(b);
  a.addEventListener("click", function(e) {
      /* 当点击自定义下拉列表时,打开或关闭下拉列表选项 */
      e.stopPropagation();
      closeAllSelect(this);
      this.nextSibling.classList.toggle("select-hide");
      this.classList.toggle("select-arrow-active");
    });
}
function closeAllSelect(elmnt) {
  /* 关闭所有下拉列表选项 */
  var x, y, i, arrNo = [];
  x = document.getElementsByClassName("select-items");
  y = document.getElementsByClassName("select-selected");
  for (i = 0; i < y.length; i++) {
    if (elmnt == y[i]) {
      arrNo.push(i)
    } else {
      y[i].classList.remove("select-arrow-active");
    }
  }
  for (i = 0; i < x.length; i++) {
    if (arrNo.indexOf(i)) {
      x[i].classList.add("select-hide");
    }
  }
}
/* 当点击页面其他地方时,关闭所有下拉列表选项 */
document.addEventListener("click", closeAllSelect);
</script>

</body>
</html>

在上述示例代码中,通过设置.select-selected:focusbackground-color属性来更改启动下拉列表激活时的背景色。你可以根据需要修改这个属性的值来改变背景色。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券