Javascript把愛心點選切換圖片

<script>
//加入收藏 或 取消收藏
function switchFavorite(){
var getHp=document.getElementById("heart");
if(getHp.title=="加入收藏"){
getHp.src="heart_red.png";
getHp.title="取消收藏";
}else {
getHp.src="heart_white.png";
getHp.title="加入收藏";
}
}

function init(){
//設定[加入收藏 或 取消收藏]的點按事件
document.getElementById("heart").onclick=switchFavorite;
  window.alert(document.getElementById("heart").src);

}//init
window.onload = init;
</script>

留言

這個網誌中的熱門文章

Use Case Description(描述使用案例)

列出不重複的隨機亂數

子類別建構子super觀念