我做了一张桌子,我想让不同的单元格都可以点击,然后出现一个模态。当模态在一行中时,我已经可以正常工作了,请参见代码段中的test3。
但是,当我在多行中创建模式时,它不会使整个单元格都可单击,而只是其中一部分。当我将鼠标悬停在其上方时,它只会更改可单击部分的颜色,但是我希望整个单元格都可单击,因此当我将鼠标悬停在其上方时会更改颜色。就像“ test3”一样。
如何更改它以使其覆盖整个单元格?
var btn = document.querySelectorAll("button.modal-button");
var modals = document.querySelectorAll('.modal');
// Get the <span> element that closes the modal
var spans = document.getElementsByClassName("close");
// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
btn[i].onclick = function(e) {
e.preventDefault();
modal = document.querySelector(e.target.getAttribute("href"));
modal.style.display = "block";
}
}
// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
spans[i].onclick = function() {
for (var index in modals) {
if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";
}
}
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target.classList.contains('modal')) {
for (var index in modals) {
if (typeof modals[index].style !== 'undefined') modals[index].style.display = "none";
}
}
}
#tableone {
background-color: transparent;
border-collapse: collapse;
width: 100%;
}
#tableone td,
th {
border: 1px solid #000000;
padding: 8px;
}
#tableone th {
padding: 8px;
text-align: left;
border: 1px solid #000000;
background-color: rgba(0, 110, 167, 1);
color: white;
}
.modalTD {
padding: 0!important;
background-color: #00a6d6;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 10px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: black;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: white;
text-decoration: none;
cursor: pointer;
}
.notebook {
border-collapse: collapse;
width: 70%;
}
.notebook td.modalTD:hover .block:hover {
background-color: yellow;
color: black;
}
.notebook td,
.notebook th {
border: 1px solid #ddd;
padding: 0px;
}
.block {
display: block;
width: 100%;
height: 100%;
border: none;
background-color: #00a6d6;
padding: 14px 28px;
font-size: 16px;
cursor: pointer;
text-align: center;
box-sizing: border-box;
}
<div class="centerjustify">
<h2>Table 1</h2>
<br>
<table id="tableone" class="notebook">
<tr id="category">
<th style="background-color:transparent; color:black;"></th>
<th style="background-color:transparent; color:black;" colspan=2>One</th>
<th style="background-color:transparent; color:black;" colspan=2>Two</th>
</tr>
<tr>
<td class="month">April</td>
<td rowspan=4 class="modalTD">
<!-- Trigger/Open The Modal -->
<button class="block modal-button" href="#myModal1">Test</button>
<!-- The Modal -->
<div id="myModal1" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</td>
<td rowspan=3 class="modalTD">
<!-- Trigger/Open The Modal -->
<button class="block modal-button" href="#myModal2">Test2</button>
<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</td>
<td colspan=2></td>
</tr>
<tr>
<td class="month">May</td>
<td colspan=2></td>
</tr>
<tr>
<td class="month">June</td>
<td colspan=2></td>
</tr>
<tr>
<td class="month">July</td>
<td></td>
<td colspan=2 class="modalTD">
<!-- Trigger/Open The Modal -->
<button class="block modal-button" href="#myModal3">Test 3</button>
<!-- The Modal -->
<div id="myModal3" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-body">
<p>Some text in the Modal Body</p>
<p>Some other text...</p>
</div>
</div>
</div>
</td>
</tr>
</table>
</div>
只需更改CSS。
td.modalTD:hover {
background-color: yellow;
}
td.modalTD:hover .block {
background-color: yellow;
}
当您将鼠标悬停在td上时,td和块的背景颜色都会改变。