javascript生成动态表格,并为每个单元格添加单击事件

 
 
 
 
<html>
  <head>
    <title>Demo</title>
  </head> 
  <body>   
	<label style="font-size:20px;width:600px;" >动态表格:</label><br/>
	<table border="1">
		<tbody id="table">  
	</table>
  </body>
</html>

以下是使用JavaScript生成可编辑能够插入、改变大小、点击放大的网页表格的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Editable Table with Resizable and Zoomable Images</title> <style> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 5px; text-align: center; } th { background-color: #f2f2f2; } td { position: relative; } .resizable { border: 2px dotted black; position: absolute; right: 0; bottom: 0; width: 20px; height: 20px; cursor: nwse-resize; } .zoomable { cursor: zoom-in; max-width: 100%; max-height: 100%; } .overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: none; justify-content: center; align-items: center; } .overlay img { max-width: 90%; max-height: 90%; } </style> </head> <body> <h1>Editable Table with Resizable and Zoomable Images</h1> <table id="editable-table"> <thead> <tr> <th>Name</th> <th>Image</th> </tr> </thead> <tbody> <tr> <td contenteditable="true">Product 1</td> <td> <div class="image-container"> <img class="zoomable" src="" alt="No Image"> <div class="resizable"></div> </div> </td> </tr> <tr> <td contenteditable="true">Product 2</td> <td> <div class="image-container"> <img class="zoomable" src="" alt="No Image"> <div class="resizable"></div> </div> </td> </tr> </tbody> </table> <div class="overlay"> <img src="" alt=""> </div> <script> const resizable = document.querySelectorAll('.resizable'); const zoomable = document.querySelectorAll('.zoomable'); const overlay = document.querySelector('.overlay'); const overlayImage = document.querySelector('.overlay img'); // Make images resizable resizable.forEach(handle => { const container = handle.parentElement; const image = container.querySelector('.zoomable'); let startX, startY, startWidth, startHeight; const handleResize = event => { const newWidth = startWidth + (event.clientX - startX); const newHeight = startHeight + (event.clientY - startY); container.style.width = `${newWidth}px`; container.style.height = `${newHeight}px`; image.style.width = `${newWidth}px`; image.style.height = `${newHeight}px`; }; handle.addEventListener('mousedown', event => { event.preventDefault(); event.stopPropagation(); startX = event.clientX; startY = event.clientY; startWidth = parseInt(document.defaultView.getComputedStyle(container).width, 10); startHeight = parseInt(document.defaultView.getComputedStyle(container).height, 10); document.addEventListener('mousemove', handleResize); document.addEventListener('mouseup', () => { document.removeEventListener('mousemove', handleResize); }); }); }); // Make images zoomable zoomable.forEach(image => { image.addEventListener('click', () => { overlayImage.src = image.src; overlay.style.display = 'flex'; }); }); // Close overlay on click overlay.addEventListener('click', () => { overlay.style.display = 'none'; }); </script> </body> </html> ``` 该代码会生成一个带有两行可编辑的表格,每行包含一个可编辑的文本单元格和一个包含可调整大小和可放大的图像的单元格。当用户选择图像并上传时,JavaScript代码会在单元格中显示图像,并允许用户拖动边缘来调整大小,以及单击图像以放大它。单击放大的图像会使其在另一个覆盖层中显示。您可以根据需要添加更多行或自定义样式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值