html 엘리멘탈 이벤트 문의. 정보
개발자 html 엘리멘탈 이벤트 문의.본문
<div>
<div></div>
</div>
div안에 있는 div가 제거될시 이벤트가 발생되게 하고 싶은데..
이런 이벤트가 있나요?
추천
0
0
댓글 3개
오브젝트가 destroy될 때 이벤트 발생관련하는게 원래 자바스크립트에는 없습니다. 다만 setTimeout을 이용해서 만들 수는 있습니다. 다행히 jQuery에 destroyed라는 event를 추가해주는 플러그인을 만든 사람이 있네요. 이걸 이용하면 쉽게 짤 수 있습니다. 예제소스를 만들어보았으니 테스트해보면 쉽게 알수 있어요. 원리도 간단합니다. 단순하게 bind로 destroyed event 추가만 해주면 되요.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Test for jQuery event.destroyed </title>
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="Jacob Lee" />
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<!--<script src="jquery.event.destroyed.js"
type="text/javascript"></script>-->
<script src="http://v3.javascriptmvc.com/jquery/dist/jquery.event.destroyed.js"
type="text/javascript"></script>
</head>
<body>
<!-- Test for jQuery event.destroyed
When a object is destroyed, the event will show -->
<div class="container">
<div class="hello">Hello</div>
<div class="goodbye">Goodbye</div>
</div>
<button onclick="destroy_hello();">Click Me!</button>
<script type="text/javascript">
$(document).ready(function(){
$('.hello').bind('destroyed', function(){alert('goodbye "Hello"!');});
});
function destroy_hello()
{
$('.hello').remove();
}
</script>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Test for jQuery event.destroyed </title>
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="Jacob Lee" />
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<!--<script src="jquery.event.destroyed.js"
type="text/javascript"></script>-->
<script src="http://v3.javascriptmvc.com/jquery/dist/jquery.event.destroyed.js"
type="text/javascript"></script>
</head>
<body>
<!-- Test for jQuery event.destroyed
When a object is destroyed, the event will show -->
<div class="container">
<div class="hello">Hello</div>
<div class="goodbye">Goodbye</div>
</div>
<button onclick="destroy_hello();">Click Me!</button>
<script type="text/javascript">
$(document).ready(function(){
$('.hello').bind('destroyed', function(){alert('goodbye "Hello"!');});
});
function destroy_hello()
{
$('.hello').remove();
}
</script>
</body>
</html>
정말로 고맙습니다.
jQuery에 대해서 어느 정도 안다고 생각하고 있었는데 이런 것도 있군요.
좋은 정보 감사합니다.
좋은 정보 감사합니다.