jQuery之detach的使用

detach

detach与remove不同的是,他不会销毁element及其绑定的事件,在需要利用这些信息的时候,便是使用detach的时机。

事件命名空间+detach

	<html>
	<head>
	<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
	<script type="text/javascript">
	$(document).ready(function(){
	var p;
	function myEventHandler(){
	   console.log(1);
	   p=$("p").detach();
	}
	function myEventHandler2(){
	   console.log(2);
	# 延时看下效果
	   setTimeout(function(){ 		        
	   	$('body').append(p);
	    },3000);
	}
	$("button").on("click.m", myEventHandler); // 不错

	$("button").on("click.v", myEventHandler2); 

	// 之后,让我们优雅地解除绑定
	//$("button").unbind("click.m");

	});
	</script>
	</head>

	<body>
	<p>This is a paragraph.</p>
	<button>删除 p 元素</button>
	</body>
	</html>
Questions? Leave a comment below or email: newbvirgil@gmail.com