jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 的 remove()
方法用于移除被选元素及其子元素。
jQuery 的 remove()
方法属于 DOM 操作方法,用于移除元素。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Remove Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="container">
<button id="removeBtn">Remove Element</button>
<div id="elementToRemove">This element will be removed.</div>
</div>
<script>
$(document).ready(function() {
$('#removeBtn').click(function() {
$('#elementToRemove').remove();
});
});
</script>
</body>
</html>
remove()
方法后,元素没有被移除?原因:
解决方法:
$(document).ready()
确保 DOM 加载完成后再执行脚本。$(document).ready()
确保 DOM 加载完成后再执行脚本。通过以上方法,可以确保 jQuery 的 remove()
方法正确移除元素。