js下拉框跳转到对应的网址不好用,有时候点击无效,所以用css样式+a标签模拟写一个下拉框点击选项跳转不同链接的效果
让模拟的下拉框看起来像一个标准的<-select>元素,可以使用一些CSS来添加边框和箭头图标。以下改进的代码:
html1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Dropdown</title>
<style>
.dropdown {
position: relative;
display: inline-block;
width: 200px;
}
.dropbtn {
display: inline-block;
width: 100%;
padding: 10px;
border: 1px solid #ccc;
background-color: white;
cursor: pointer;
text-align: left;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
position: relative;
}
.dropbtn::after {
content: '▼';
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
pointer-events: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
border: 1px solid #ccc;
}
.dropdown-content a {
color: black;
padding: 10px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<div class="dropdown">
<div class="dropbtn">Select an option</div>
<div class="dropdown-content">
<a href="https://www.example1.com">Option 1</a>
<a href="https://www.example2.com">Option 2</a>
<a href="https://www.example3.com">Option 3</a>
</div>
</div>
</body>
</html>