切换按钮样式以及可以切换某一容器中的图片
再一次工作中遇到的问题,将两种功能进行了一次结合,并且记录了一下。
直接上代码了:
html:
//要切换的图片
<img id="img0" src="../image/home/shangpin1@3x.png" alt="" id="tuoanlinggai">
//按钮
<div class="itemb" tapmode onclick="yanse();">
<img style="" src="../image/cpxq/qiehuan/xing@3x.png" alt="">
<span id="color">水电费水电费</span>
</div>
css:
.itemb{
height: 1.5rem;
display: flex;
justify-content:flex-start;
align-items: center;
background-color: #f8f8f8;
border-radius: 0.5rem;``
padding:0 0.4rem;
margin-right: 0.3rem;
margin-bottom: 0.5rem;
}
.itemb-active{
border: solid 0.01rem #33ccff;
color:#33ccff;
height: 1.5rem;
display: flex;
justify-content:flex-start;
align-items: center;
background-color: #f8f8f8;
border-radius: 0.5rem;
/*padding:0 0.4rem;*/
margin-right: 0.3rem;
margin-bottom: 0.5rem;
}
js:
function yanse() {
isClick = true;
// 获取容器内容并且切换样式
$('.itemb').mouseover(function() {
$(this).addClass('itemb-active');
var yance1 = $("#color").text();
var yance2 = $("#chicun").text();
color = yance1;
chicun = yance2;
}).mouseout(function() {
$(this).removeClass('itemb-active');
})
// chicunname = shu.innerText;
// 切换图片
var img = document.getElementById("img0")
// img.setAttribute("src","../image/cpxq/qiehuan/xing@3x.png");
img.src = "../image/cpxq/qiehuan/xing@3x.png";
}
图片路径是本人自己写的,用的话记得更改图片路径。
这篇博客介绍了如何结合HTML、CSS和JavaScript实现点击切换按钮来改变按钮样式,同时能切换容器内的图片。提供了详细的代码示例,注意实际使用时需调整图片路径。

1万+

被折叠的 条评论
为什么被折叠?



