jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。弹出表格通常是指在用户交互(如点击按钮)时,动态显示一个表格。
以下是一个简单的 jQuery 弹出表格的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Popup Table</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
#popupTable {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid #ccc;
background-color: #fff;
}
</style>
</head>
<body>
<button id="showTableBtn">Show Table</button>
<div id="popupTable">
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John Doe</td>
<td>30</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>25</td>
</tr>
</table>
</div>
<script>
$(document).ready(function() {
$('#showTableBtn').click(function() {
$('#popupTable').fadeIn();
});
$(document).on('click', function(event) {
if (!$(event.target).closest('#popupTable, #showTableBtn').length) {
$('#popupTable').fadeOut();
}
});
});
</script>
</body>
</html>
display: none
。event.stopPropagation()
阻止事件冒泡。position: absolute
和 transform
属性来精确控制弹出表格的位置。通过以上示例代码和解决方法,你应该能够实现一个基本的 jQuery 弹出表格,并解决常见的相关问题。
领取专属 10元无门槛券
手把手带您无忧上云