原型大概是這樣的
image
需求:點擊右側(cè)的藍色眼睛奥帘,會變成
關(guān)鍵代碼:
/* 實現(xiàn)圖片切換 */
$(".imgclick").toggle(function(){
$(this).attr("src","img/close.png");
},function(){
$(this).attr("src","img/open.png");
}).attr("src","img/open.png");
其實原理很簡單,就是利用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>
/* 實現(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”已旧,拉你進程序員技術(shù)討論群
</html>
原文作者:祈澈姑娘
技術(shù)博客:http://www.reibang.com/u/05f416aefbe190后前端妹子秸苗,愛編程,愛運營运褪,愛折騰惊楼。
堅持總結(jié)工作中遇到的技術(shù)問題,堅持記錄工作中所所思所見秸讹,歡迎大家一起探討交流檀咙。