remove()方法的定義和用法:
此方法將會從DOM中刪除所有匹配的元素。
說明:remove()方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素,不過除了這個元素本身得以保留之外,其他的比如綁定的事件,附加的數據等都會被移除。
語法結構:
$(selector).remove(exPR)
參數列表:
| 參數 | 描述 | 
| expr | 可選。用于篩選元素的jQuery表達式 | 
實例代碼:
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>螞蟻部落</title><script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/Javascript"> $(document).ready(function(){ $("button").click(function(){ $("div").remove("#first"); })})</script> </head><body><div id="first">我是第一</div><div id="second">我是第二</div><button>點擊</button></body></html>
以下代碼能夠刪除div集合中id為first的div。
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>螞蟻部落</title><script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript"> $(document).ready(function(){ $("#btd").click(function(){ $("div").remove(); })})</script> </head><body><div id="first">我是第一</div><div id="second">我是第二</div><button id="btd">點擊刪除第一個div</button></body></html>
如果省略參數,那就是刪除所有匹配的元素。
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>螞蟻部落</title><style type="text/CSS">div{ width:200px; height:200px; border:5px solid green;}</style><script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript"> $(document).ready(function(){ $("#btd").click(function(){ var a=$("div"); a.remove("#first"); $("#btn").click(function(){ alert(a.length); }) })})</script> </head><body><div id="first">我是第一</div><div id="second">我是第二</div><button id="btd">刪除第一個div</button><button id="btn">查看刪除操作后div的數量</button></body></html>
remove()已經將DOM中的匹配元素刪除,但是并沒有將它從jquery對象中刪除。
原文地址是:http://www.51texiao.cn/jqueryjiaocheng/2015/0613/4051.html
最為原始地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=5425
新聞熱點
疑難解答