我現在處於一個大麻煩。在這裏,我有兩個有周日和週一例程的表格,我使用下面的代碼來突出顯示當前時間的任何行的背景。 它只突出顯示Sun的行,而不是Mon,或者如果我在週日的代碼編輯器中選擇Mon table,那麼它僅適用於星期一,並且不會突出顯示星期日的行。使用Javascript與兩個同一類的兩個div
這裏有什麼問題?任何幫助將主要讚賞。
這裏是我的代碼的現場演示
https://jsbin.com/desofuvevu/edit?html,css,js,output
function openCity(evt, cityName, today) {
var i, tabcontent, tablinks;
tabcontent =
document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
if (typeof today == 'undefined') {
evt.currentTarget.className += " active";
} else {
tablinks[today].className += " active";
}
}
let now = new Date().getHours() * 100 + new
Date().getMinutes();
let times = [45, 100, 145];
let ids = ['ra1', 'ra3', ];
let selected = ids[times.reduce((acc, curr, idx) => now >=
curr ? idx : acc)];
if (selected)
document.getElementById(selected).style.backgroundColor = 'red';
.tabcontent { display: none }
<div class="tab" style="margin-top: 1.6%; ">
<button class="tablinks" onclick="openCity(event, 'Sun0') ">Sun</button>
<button class="tablinks" onclick="openCity(event, 'Mon0')">Mon</button>
</div>
<div id="Sun0" class="tabcontent" id="np">
<div style="overflow-x:auto;">
<table>
<col width="65%">
<col width="35%">
<tr>
<th>Class</th>
<th>Time</th>
</tr>
<tr>
<td id="ra1">10:15-11:00AM</td>
<td style="background:black; color:Yellow;">It is Sunday </td>
</tr>
<tr>
<td id="ra3">11:00-11:45AM</td>
<td style="background:black; color:Yellow;">It is Sunday</td>
</tr>
</table>
</div>
</div>
<div id="Mon0" class="tabcontent">
<!--Monnday-->
<div style="overflow-x:auto;">
<table>
<col width="65%">
<col width="35%">
<tr>
<th>Class</th>
<th>Time</th>
</tr>
<tr>
<td id="ra1">10:15-11:00AM</td>
<td style="background:black; color:Yellow;">It is Monday</td>
</tr>
<tr>
<td id="ra3">11:00-11:45AM</td>
<td style="background:black; color:Yellow;">It is Monday</td>
</tr>
</table>
</div>
</div>
我創建了一個片段爲你。請添加您遇到問題的腳本以創建[mcve] – mplungjan
是的,我忘記了其中一個,現在我已重新編輯了我的帖子,是的,我也會查看您的片段 –
我已將您的代碼段重新添加。問題是什麼? – mplungjan