IT/jQuery

jQuery - bind 이벤트 연결 (mouseover, click), 메소드 체이닝

노마드오브 2018. 11. 16. 20:40

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

// 이벤트  bind()  click()  mouserover()  

//$('선택자').bind('이벤트명', 이벤트처리함수정의);

$('h1').bind('mouseover', function () {

//$(this).append('+');

//$(this).html('<b>aaa</b>');

$(this).html(function (index, oldHtml) {

return oldHtml + '+';

});

});

// click 이벤트

//$('div').bind('click', function () {});

$('div').click(function () {

$(this).append("*");

});

});

</script>

</head>

<body>

<div>div0</div>

<div>div1</div>

<div>div2</div>

<h1>head0</h1>

<h1>head1</h1>

<h1>head2</h1>

</body>

</html>




<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!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>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

// 메소드 체이닝으로 호출이 가능

// 대상.mouseover().mouseout();

// mouseover mouseout 이벤트

$('.img1').mouseover(function () {

$(this).attr('src', '../j1/dog.jpg');

}).mouseout(function () {

$(this).attr('src', '../j1/cat.png');

});

});

</script>

</head>

<body>

<img class="img1" src="../j1/cat.png" width="100" height="100">

</body>

</html>