如何在 onclick 上更改 imagebutton 背景
How can i change imagebutton background on onclick
我有图像按钮,我想在按下按钮时更改按钮的背景图像。起初我有绿色刻度标志,当我按下图像按钮时,绿色刻度标志应该变成红色刻度标志。如果我再次按下它,它应该变成 绿色勾号标志 。我再次按下它,它应该再次变为 红色刻度标志 并在我再次按下它时重复。帮助我 (:
试试这个:
var clicked = 0
$("#button").click(function() {
if (clicked == 0) {
$(this).css("background", "url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQm19_RGu6ZbKhn_uryzGqZLIldxuLIYX-m6A&usqp=CAU) no-repeat");
clicked = 1;
} else {
$(this).css("background", "url(https://lesspestcontrol.com.au/wp-content/uploads/green-tick.png) no-repeat");
clicked = 0;
}
})
.button {
width: 250px;
height: 250px;
background: url(https://lesspestcontrol.com.au/wp-content/uploads/green-tick.png) no-repeat;
cursor: pointer;
border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
</head>
<body>
<input id="button" type="button" name="button" value="Search" class="button" />
</body>
</html>
注意:我将按钮变大了,这样它就能适应图片。
我有图像按钮,我想在按下按钮时更改按钮的背景图像。起初我有绿色刻度标志,当我按下图像按钮时,绿色刻度标志应该变成红色刻度标志。如果我再次按下它,它应该变成 绿色勾号标志 。我再次按下它,它应该再次变为 红色刻度标志 并在我再次按下它时重复。帮助我 (:
试试这个:
var clicked = 0
$("#button").click(function() {
if (clicked == 0) {
$(this).css("background", "url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQm19_RGu6ZbKhn_uryzGqZLIldxuLIYX-m6A&usqp=CAU) no-repeat");
clicked = 1;
} else {
$(this).css("background", "url(https://lesspestcontrol.com.au/wp-content/uploads/green-tick.png) no-repeat");
clicked = 0;
}
})
.button {
width: 250px;
height: 250px;
background: url(https://lesspestcontrol.com.au/wp-content/uploads/green-tick.png) no-repeat;
cursor: pointer;
border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<head>
</head>
<body>
<input id="button" type="button" name="button" value="Search" class="button" />
</body>
</html>
注意:我将按钮变大了,这样它就能适应图片。