본문 바로가기

JQuery/work

사진 클릭하여 이미지 변경하기(attr)

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<h1>Room Image</h1>
<img alt="" src="./images/b_pic2.jpg" id="pic">

<script type="text/javascript">
$(function () {
	/*
	let num = 0;	
	$("#pic").click(function() {
		if(num == 0){
			$(this).attr("src", "./images/b_pic1.jpg");
			num = 1;
		}
		else{
			$(this).attr("src", "./images/b_pic2.jpg");
			num = 0;
		}		
	});
	*/
	$("#pic").hover(
		function () {	// mouseover
			$(this).attr("src", "./images/b_pic1.jpg");
		},
		function () {	// mouseout
			$(this).attr("src", "./images/b_pic2.jpg");
		}		
	);
});
</script>
</body>
</html>