原型大概是這樣的
image
需求:點(diǎn)擊右側(cè)的藍(lán)色眼睛,會變成
關(guān)鍵代碼:
/* 實(shí)現(xiàn)圖片切換 */
$(".imgclick").toggle(function(){
$(this).attr("src","img/close.png");
},function(){
$(this).attr("src","img/open.png");
}).attr("src","img/open.png");
其實(shí)原理很簡單,就是利用toggle的參數(shù)切換不同函數(shù)。
可以直接復(fù)制黏貼的demo
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
</head>
<body>
<div>
<img class="imgclick" src="img/close.png" />
</div>
<div>
<img class="imgclick" src="img/close.png" />
</div>
</body>
<script>
/* 實(shí)現(xiàn)圖片切換 */
$(".imgclick").toggle(function() {
$(this).attr("src", "img/close.png");
}, function() {
$(this).attr("src", "img/open.png");
}).attr("src", "img/open.png");
</script>
//關(guān)注「編程微刊」公眾號 ,在微信后臺回復(fù)「領(lǐng)取資源」,獲取IT資源200G干貨大全。公眾號回復(fù)“1”,拉你進(jìn)程序員技術(shù)討論群
</html>
原文作者:祈澈姑娘
技術(shù)博客:http://www.lxweimin.com/u/05f416aefbe190后前端妹子,愛編程,愛運(yùn)營,愛折騰。
堅持總結(jié)工作中遇到的技術(shù)問題,堅持記錄工作中所所思所見,歡迎大家一起探討交流。