写一个例子 tab点击让tab里面icon图片变颜色
时间: 2023-06-05 12:08:41 浏览: 171
好的,下面是一个实现tab点击让tab里面icon图片变颜色的例子代码:
HTML部分:
```
<div class="tab">
<button class="tablinks" onclick="openTab(event, 'home')">
<img src="home.png" id="home-icon">
Home
</button>
<button class="tablinks" onclick="openTab(event, 'about')">
<img src="about.png" id="about-icon">
About
</button>
</div>
<div id="home" class="tabcontent">
<h3>Home Page</h3>
<p>This is the home page content</p>
</div>
<div id="about" class="tabcontent">
<h3>About Page</h3>
<p>This is the about page content</p>
</div>
```
CSS部分:
```
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
.tab button:hover {
background-color: #ddd;
}
.tab button.active {
background-color: #ccc;
}
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
```
JS部分:
```
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
tablinks[i].querySelector("img").style.filter = "grayscale(100%)";
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
evt.currentTarget.querySelector("img").style.filter = "none";
}
```
注意,在CSS中,我们使用了CSS滤镜来处理图标的颜色。在JS中,我们通过添加或移除CSS类来实现选项卡的切换,然后使用querySelector和style属性来更改图标的滤镜颜色。
阅读全文