当我选择值为 1(万事达卡)的选项时,如何才能将 id Master 的图像的不透明度更改为 1?并将其放入一个适用于这 4 个值的函数中,每个值都有自己的 id?我希望默认图像为 0.3,当选择其中一个选项时,图像的不透明度为 1。
.itemForm2 {
width: 100%;
position: relative;
margin-bottom: 18px;
margin-top: 10px;
display: flex;
flex-direction: row;
}
.escolhaCartao {
display: flex;
flex-direction: row;
width: 218px!important;
gap: 10px;
}
@media(min-width:768px) {
.escolhaCartao {
width: 258px!important;
height: 34.32px;
gap: 5px;
}
.lineFormCartao {
display: flex;
flex-direction: row;
justify-content: inherit;
gap: 16px;
}
.escolhaCartao img {
opacity: .3;
}
}
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
仅使用 CSS 和 has
.escolhaCartao img { opacity: .2; } .itemForm:has(option[value="1"]:checked) + .itemForm2 > #master, .itemForm:has(option[value="2"]:checked) + .itemForm2 > #visa, .itemForm:has(option[value="3"]:checked) + .itemForm2 > #elo, .itemForm:has(option[value="4"]:checked) + .itemForm2 > #amex { opacity: 1; }使用 JavaScript
var sel = document.querySelector("#OpcoesCartao"); const imgs = document.querySelectorAll(".escolhaCartao img"); sel.addEventListener("change", function (e) { const val = this.value; const selected = document.querySelector(".escolhaCartao img.selected"); if (selected) { selected.classList.remove('selected'); } imgs[+val - 1].classList.add('selected'); }); sel.dispatchEvent(new Event('change'));.escolhaCartao img { opacity: .2; } .escolhaCartao img.selected { opacity: 1; }