代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>可拖动输入和校验的表格</title>
<style>
html, body, div, p, table, td, th {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.table {
border-collapse: collapse;
}
.table,
.table td,
.table th {
height: 40px;
min-width: 120px;
border: 1px solid #ccc;
}
.table th {
position: relative;
}
.resizer {
/* Displayed at the right side of column */
position: absolute;
top: 0;
right: 0;
width: 5px;
cursor: col-resize;
user-select: none;
}
.resizer:hover,
.resizing {
border-right: 2px solid blue;
}
/* td内部元素样式 */
td > div {
/* border: none; */
display: flex;
width: 100%;
height: 100%;
outline: none;
}
td > div > input {
flex: 1;
/* min-width: 80px; */
width: 80px;
height: 100%;
outline: none;
padding: 0 8px;
border-radius: 0;
}
td > div > input:focus {
box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}
</style>
</head>
<body>
<table class="table" id="resizeMe">
<thead>
<tr>
<th>No.</th>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
<!-- 创建表格 -->
<script>
const tbody = document.getElementById('tbody')
const names = Array(12).fill('Andrea Ross')
names.forEach((item, i) => {
const tr = document.createElement('tr')
const arr = item.split(' ')
arr.unshift(i)
arr.forEach((item, j) => {
const td = document.createElement('td')
// td.innerText = item
const div = document.createElement('div')
const input = document.createElement('input')
// input.value = `${i} - ${j}`
input.pattern = '[0-9]{3}'
div.appendChild(input)
td.appendChild(div)
tr.appendChild(td)
})
tbody.appendChild(tr)
})
</script>
<!-- 实现拖动大小 -->
<script>
const table = document.getElementById('resizeMe');
// Query all headers
const cols = table.querySelectorAll('th');
const createResizableColumn = function (col, resizer) {
// Track the current position of mouse
let x = 0;
let w = 0;
const mouseDownHandler = function (e) {
// Get the current mouse position
x = e.clientX;
// Calculate the current width of column
const styles = window.getComputedStyle(col);
w = parseInt(styles.width, 10);
// Attach listeners for document's events
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
resizer.classList.add('resizing');
};
const mouseMoveHandler = function (e) {
// Determine how far the mouse has been moved
const dx = e.clientX - x;
// Update the width of column
col.style.width = `${w + dx}px`;
};
// When user releases the mouse, remove the existing event listeners
const mouseUpHandler = function () {
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
resizer.classList.remove('resizing');
};
resizer.addEventListener('mousedown', mouseDownHandler);
};
// Loop over them
[].forEach.call(cols, function (col) {
// Create a resizer element
const resizer = document.createElement('div');
resizer.classList.add('resizer');
// Set the height
resizer.style.height = `${table.offsetHeight}px`;
// Add a resizer element to the column
col.appendChild(resizer);
// Will be implemented in the next section
createResizableColumn(col, resizer);
});
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。