请帮助简化功能,是否可以只使用一个功能?提前致谢。
function svisa(){
document.getElementById('visa').style.border=".2vw solid red";
document.getElementById('qiwi').style.border=".2vw solid grey";
document.getElementById('mastercard').style.border=".2vw solid grey";
document.getElementById('webmoney').style.border=".2vw solid grey";
document.getElementById('yandex').style.border=".2vw solid grey";
}
function sqiwi(){
document.getElementById('visa').style.border=".2vw solid grey";
document.getElementById('qiwi').style.border=".2vw solid red";
document.getElementById('mastercard').style.border=".2vw solid grey";
document.getElementById('webmoney').style.border=".2vw solid grey";
document.getElementById('yandex').style.border=".2vw solid grey";
}
function smastercard(){
document.getElementById('visa').style.border=".2vw solid grey";
document.getElementById('qiwi').style.border=".2vw solid grey";
document.getElementById('mastercard').style.border=".2vw solid red";
document.getElementById('webmoney').style.border=".2vw solid grey";
document.getElementById('yandex').style.border=".2vw solid grey";
}
function swebmoney(){
document.getElementById('visa').style.border=".2vw solid grey";
document.getElementById('qiwi').style.border=".2vw solid grey";
document.getElementById('mastercard').style.border=".2vw solid grey";
document.getElementById('webmoney').style.border=".2vw solid red";
document.getElementById('yandex').style.border=".2vw solid grey";
}
function syandex(){
document.getElementById('visa').style.border=".2vw solid grey";
document.getElementById('qiwi').style.border=".2vw solid grey";
document.getElementById('mastercard').style.border=".2vw solid grey";
document.getElementById('webmoney').style.border=".2vw solid grey";
document.getElementById('yandex').style.border=".2vw solid red";
}
*{
margin: 0;
padding: 0;
}
.container{
width: 80%;
height: 45vw;
margin: 0 auto;
border: .4vw solid grey;
border-radius: 24px;
overflow: hidden;
}
.block_left{
width: 40%;
height: 100%;
float: left;
}
.block_product{
width: 100%;
height: 100%;
text-align: center;
}
span{
font-size: 2vw;
font-family: 'Acme', sans-serif;
display: block;
}
.block_right{
width: 60%;
height: 100%;
float: right;
}
.block_select{
width: 100%;
height: 30%;
text-align: center;
}
.method{
height: 25%;
width: 15%;
border: .2vw solid grey;
border-radius: 6px;
display: inline-block;
background-position: center;
background-size: cover;
}
.visa{
background-image: url('image/visa.png');
}
.qiwi{
background-image: url('image/qiwi.png');
}
.mastercard{
background-image: url('image/mastercard.png');
}
.webmoney{
background-image: url('image/webmoney.png');
}
.yandex{
background-image: url('image/yandex.png');
}
.block_info{
width: 100%;
height: 40%;
}
.block_confirm{
width: 100%;
height: 30%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://fonts.googleapis.com/css2?family=Acme&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/4fcf22c1ce.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
<title>Pay</title>
</head>
<body>
<div class="container">
<div class="block_left">
<div class="block_product">
<span class="product">Выбранные продукты</span>
</div>
</div>
<div class="block_right">
<div class="block_select">
<span class="seletc">Выберете метод оплаты</span>
<div class="method visa" onclick="svisa()" id="visa">
</div>
<div class="method qiwi" onclick="sqiwi()" id="qiwi">
</div>
<div class="method mastercard" onclick="smastercard()" id="mastercard">
</div>
<div class="method webmoney" onclick="swebmoney()" id="webmoney">
</div>
<div class="method yandex" onclick="syandex()" id="yandex">
</div>
</div>
<div class="block_info">
</div>
<div class="block_confirm">
</div>
</div>
</div>
</body>
</html>
在上面的例子中,一个有点旧的代码,所以你的页面上没有任何东西突然中断。
有用的链接:
• http://learn.javascript.ru/
• https://developer.mozilla.org/ru/
这是有效的,因为在 CSS 中,代码中下面的选择器优先(其他条件相同)。