jQuery的remove()方法使用詳解
remove()方法的定義和用法:
此方法將會(huì)從DOM中刪除所有匹配的元素。
說明:remove()方法不會(huì)把匹配的元素從jQuery對(duì)象中刪除,因而可以在將來再使用這些匹配的元素,不過除了這個(gè)元素本身得以保留之外,其他的比如綁定的事件,附加的數(shù)據(jù)等都會(huì)被移除。
語法結(jié)構(gòu):
$(selector).remove(expr)
參數(shù)列表:
參數(shù) 描述
expr 可選。用于篩選元素的jQuery表達(dá)式
實(shí)例代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <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>點(diǎn)擊</button> </body> </html>
以下代碼能夠刪除div集合中id為first的div。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <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">點(diǎn)擊刪除第一個(gè)div</button> </body> </html>
如果省略參數(shù),那就是刪除所有匹配的元素。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <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">刪除第一個(gè)div</button><button id="btn">查看刪除操作后div的數(shù)量</button> </body> </html>
remove()已經(jīng)將DOM中的匹配元素刪除,但是并沒有將它從jquery對(duì)象中刪除。
jquery使用remove()方法刪除指定class子元素的方法
<!DOCTYPE html> <html> <head> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").remove(".italic"); }); }); </script> </head> <body> <p>This is a paragraph in the div.</p> <p class="italic"><i>This is another paragraph in the div.</i></p> <p class="italic"><i>This is another paragraph in the div.</i></p> <button>Remove all p elements with class="italic"</button> </body> </html>
看到這段代碼之后,我想不用我過多的解釋了。大家就明白了吧,很有意思的方法。
相關(guān)文章
使用jquery提交form表單并自定義action的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄褂胘query提交form表單并自定義action的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05jQuery的選擇器中的通配符[id^=''code'']或[name^=''code'']及jquery選擇器總結(jié)
這篇文章主要介紹了jQuery的選擇器中的通配符[id^='code']或[name^='code']及jquery選擇器總結(jié)的相關(guān)資料,需要的朋友可以參考下2015-12-12用jquery中插件dialog實(shí)現(xiàn)彈框效果實(shí)例代碼
這篇文章介紹了jquery中插件dialog實(shí)現(xiàn)彈框效果實(shí)例代碼,有需要的朋友可以參考一下2013-11-11jquery+ajax實(shí)現(xiàn)注冊(cè)實(shí)時(shí)驗(yàn)證實(shí)例詳解
這篇文章主要介紹了jquery+ajax實(shí)現(xiàn)注冊(cè)實(shí)時(shí)驗(yàn)證的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery基于ajax請(qǐng)求實(shí)現(xiàn)注冊(cè)時(shí)無刷新驗(yàn)證的相關(guān)技巧,需要的朋友可以參考下2015-12-12細(xì)說瀏覽器特性檢測(cè)(2)-通用事件檢測(cè)
在上一篇中介紹了jQuery1.4版本新增的幾個(gè)瀏覽器特性檢測(cè)方案和具體的目的,本文將以事件為中心,介紹一個(gè)較為完整、通用的事件檢測(cè)方案。2010-11-11jquery實(shí)現(xiàn)省市select下拉框的替換(示例代碼)
本篇文章主要是對(duì)jquery實(shí)現(xiàn)省市select下拉框的替換(示例代碼)進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02jquery中ajax函數(shù)執(zhí)行順序問題之如何設(shè)置同步
這篇文章主要介紹了jquery中ajax函數(shù)執(zhí)行順序問題之如何設(shè)置同步,需要的朋友可以參考下2014-02-02