本文示範如何使用 jQuery 的 remove() 方法,刪除符合指定條件的元素——這裡以 class 為例,移除帶有某 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>
寫法 $("p").remove(".italic") 的意思是:在所有 <p> 元素中,只刪除那些帶有 italic class 的段落,其餘段落保留。希望本文對大家的 jQuery 開發有所幫助。