본문 바로가기

강좌/jQuery

jQuery Attributes(jQuery로 요소의 속성 변경하기) - 2

1. 마우스 오버시 다른 이미지와 아웃시 원래 이미지 보여주기 및 attr로 속성 가져오기 예제

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>

<meta http-equiv="content-type" content="text/html;" charset="utf-8" >

        <title> jquery </title>

<script src="http://code.jquery.com/jquery-1.7.2.js"></script>

<script>

   $(document).ready(function (){

   alert($('a').attr('href')); //get

//1 : img에 마우스 오버시 이미지 변경

$('img:first').mouseover(function(){

   $(this).attr("src", "http://img.naver.net/static/www/up/2013/0419/mat_16592711c.jpg");

});

//2 ; 마우스 오버/아웃시 다른 이미지 표시

$('#copy').mouseover(function(){

   $(this).attr("src", "../images/icon_copy_over.gif");

});

$('#copy').mouseoout(function(){

   $(this).attr("src", "../images/icon_copy.gif");

});


});

</script>

    </head>


    <body>

<a href="http://www.naver.com">네이년</a>

<img src="image/logo.jpg" alt="이미지1">

<img src="image/logo.jpg" alt="이미지2" id="copy">

</body>

</html>



2. 마우스 오버시 큰 이미지 보여주기
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="content-type" content="text/html;" charset="utf-8" >
        <title> jquery </title>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script>
   $(document).ready(function (){
   //마우스 오버시 동일 이미지를 아래 showImage div에다가 확대해서 보여준다.
$('#product img').mouseover(function(){
   $('#showImage').show(); //이미지 보여줄 에이어 보이기
var imgSrc = "";
imgSrc = $(this).attr("src");//attr()로 src get하기
imgSrc = "<img src = " + imgSrc +" width=400px, height=400px>";
$('#showImage').html(imgSrc);
   
});
   $('#product img').mouseout(function(){
   $('#showImage').hide(); //이미지 보여줄 에이어 보이기
});
});
</script>
    </head>

    <body>
   <div id="product">
<img src="http://img.naver.net/static/www/up/2013/0419/mat_163915602c.jpg">

<div id="showImage" style="border:1px solid red;width:400px;height:400px;">
</div>
</div>

</body>
</html>


(로컬에 이미지가 있을때 예제)