close
detach()的教學網如下
http://www.w3school.com.cn/jquery/manipulation_detach.asp
基本上兩個都是一種刪除節點的方法,但有其差異
Ex:
(參考網址:http://www.jquerybyexample.net/2012/05/empty-vs-remove-vs-detach-jquery.html)
先簡單的說 使用detach方式刪除節點,不會移除如原先如果有定義的hover事件
,但是如果使用remove會把.hover事件一併刪除
// 請自行定義highlight的CSS名稱
javaScript
$(document).ready(function() { var dvjQuery = null; $('#btnAdd').attr('disabled','disabled');
// .hover事件(滑入、滑出) $("#dvjQuery").hover(function() { $(this).addClass('highlight'); }, function() { $(this).removeClass('highlight'); }); $('#btnRemove').click(function() { dvjQuery = $("#dvjQuery").remove(); $(this).attr('disabled','disabled'); $('#btnAdd').removeAttr('disabled'); }); $('#btnAdd').click(function() { $("#dvParent").html(dvjQuery); $(this).attr('disabled','disabled'); $('#btnRemove').removeAttr('disabled'); }); });
html:
<form> <div id="dvParent"> <div id="dvjQuery">jQuery By Example is a good blog to learn jQuery. </div> </div> <br/><br/> <input type='button' id='btnRemove' Value=' Remove Div ' /> <input type='button' id='btnAdd' Value=' Add Div ' /> <br/><br/> </form>
全站熱搜