jQuery 刪除或是清空某個HTML元素示例
jQuery使用下面兩個方法來刪除或是清空某個HTML元素。
remove() – 刪除指定的元素(包括其子元素)
empty() – 清空指定元素的子元素
例如:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Demo</title>
<script src="scripts/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$("#div1").remove();
});
});
</script>
</head>
<body>
<div id="div1" style="height: 100px; width: 300px;
border: 1px solid black; background-color: yellow;">
This is some text in the div.
<p>This is a paragraph in the div.</p>
<p>This is another paragraph in the div.</p>
</div>
<br>
<button>Remove div element</button>
</body>
</html>
empty:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JQuery Demo</title>
<script src="scripts/jquery-1.9.1.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$("#div1").empty();
});
});
</script>
</head>
<body>
<div id="div1" style="height: 100px; width: 300px;
border: 1px solid black; background-color: yellow;">
This is some text in the div.
<p>This is a paragraph in the div.</p>
<p>This is another paragraph in the div.</p>
</div>
<br>
<button>Empty the div element</button>
</body>
</html>
jQuery的remove()方法也支持一個參數(shù),可以用于過濾一些需要刪除的HTML元素。這個參數(shù)可以為任何有效的jQuery selector.
比如下面代碼只刪除class=”italic”的<p>元素:
$("p").remove(".italic");
- jQuery使用append在html元素后同時添加多項內(nèi)容的方法
- jquery動態(tài)添加帶有樣式的HTML標(biāo)簽元素方法
- jquery html動態(tài)添加的元素綁定事件詳解
- JQuery給元素添加/刪除節(jié)點比如select
- Jquery顯示、隱藏元素以及添加刪除樣式
- jQuery動態(tài)添加、刪除元素的方法
- Jquery動態(tài)添加及刪除頁面節(jié)點元素示例代碼
- jQuery添加刪除DOM元素方法詳解
- JQuery實現(xiàn)ul中添加LI和刪除指定的Li元素功能完整示例
- jQuery動態(tài)添加及刪除表單上傳元素的方法(附demo源碼下載)
- jquery html添加元素/刪除元素操作實例詳解
相關(guān)文章
jquery 獲取自定義屬性(attr和prop)的實現(xiàn)代碼
jquery中用attr()方法來獲取和設(shè)置元素屬性,attr是attribute(屬性)的縮寫,在jQuery DOM操作中會經(jīng)常用到attr(),attr()有4個表達式2012-06-06
jQuery實現(xiàn)自動切換播放的經(jīng)典滑動門效果
這篇文章主要介紹了jQuery實現(xiàn)自動切換播放的經(jīng)典滑動門效果,可實現(xiàn)tab自動定時切換的功能,涉及jQuery基于定時函數(shù)動態(tài)操作頁面元素的相關(guān)技巧,需要的朋友可以參考下2015-09-09
jQuery實現(xiàn)鼠標(biāo)經(jīng)過提示信息的地圖熱點效果
這是一個升級版本,更新了一個在IE8里的小問題,加入了提示框的內(nèi)容自動換行處理(北京點上有演示)!估計差不多該是最后樣式了。IE6、IE8、谷歌、火狐、測試正常。2015-04-04
jquery獲取并修改觸發(fā)事件的DOM元素示例【基于target 屬性】
這篇文章主要介紹了jquery獲取并修改觸發(fā)事件的DOM元素,結(jié)合實例形式分析了jQuery基于target 屬性獲取到觸發(fā)該事件的dom并修改的相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
jquery imgareaselect 使用利用js與程序結(jié)合實現(xiàn)圖片剪切
當(dāng)前在ff3下,用jquery的 width()與height()函數(shù),在不設(shè)置圖片的寬度與高度的時候,不能取到 需要在圖片load函數(shù)里面初始化才可以2009-07-07

