jQuery的remove()方法使用詳解
remove()方法的定義和用法:
此方法將會從DOM中刪除所有匹配的元素。
說明:remove()方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素,不過除了這個元素本身得以保留之外,其他的比如綁定的事件,附加的數(shù)據(jù)等都會被移除。
語法結(jié)構(gòu):
$(selector).remove(expr)
參數(shù)列表:
參數(shù) 描述
expr 可選。用于篩選元素的jQuery表達式
實例代碼:
<!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>點擊</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">點擊刪除第一個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">刪除第一個div</button><button id="btn">查看刪除操作后div的數(shù)量</button>
</body>
</html>
remove()已經(jīng)將DOM中的匹配元素刪除,但是并沒有將它從jquery對象中刪除。
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實現(xiàn)表格動態(tài)分頁實現(xiàn)代碼
項目中經(jīng)常會對表格進行分頁,以下運用jquery對用戶管理中的用戶表格進行分頁。2011-06-06
jQuery插件Echarts實現(xiàn)的雙軸圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件Echarts實現(xiàn)的雙軸圖效果,結(jié)合完整實例形式分析了Echarts插件制作雙軸圖效果的操作步驟與相關(guān)實現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
jQuery實現(xiàn)ToolTip元素定位顯示功能示例
這篇文章主要介紹了jQuery實現(xiàn)ToolTip元素定位顯示功能,結(jié)合實例形式分析了jQuery針對頁面元素屬性的動態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-11-11
無需 Flash 使用 jQuery 復(fù)制文字到剪貼板
需要做的只是引入其腳本,在HTML標(biāo)簽上賦一個“data-clipboard-target”屬性然后寫一小段JavaScript片段。為了演示假定有一個貨幣轉(zhuǎn)換應(yīng)用,在一個文本框中輸入數(shù)值時同時將兌換結(jié)果顯示在另一個文本框中,當(dāng)點擊文本框時,會觸發(fā)事件將其復(fù)制到剪貼板然后顯示一條消息。2016-04-04

