2017-08-10 54 views
0

我現在處於一個大麻煩。在這裏,我有兩個有周日和週一例程的表格,我使用下面的代碼來突出顯示當前時間的任何行的背景。 它只突出顯示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>

+0

我創建了一個片段爲你。請添加您遇到問題的腳本以創建[mcve] – mplungjan

+0

是的,我忘記了其中一個,現在我已重新編輯了我的帖子,是的,我也會查看您的片段 –

+0

我已將您的代碼段重新添加。問題是什麼? – mplungjan

回答

1

在你的代碼不應該使用相同的ID爲多個元素,選擇將只返回第一在這種情況下匹配元素。使用類,而不是和document.getElementsByClassName()將返回元素的列表,你將不得不遍歷列表,併爲每個元素設置樣式,這樣的事情:

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 classes = ['ra1', 'ra3', ]; 
 
let selected = classes[times.reduce((acc, curr, idx) => now >= 
 
    curr ? idx : acc)]; 
 
if (selected){ 
 
    var elements = document.getElementsByClassName(selected); 
 
    for(var i=0;i<elements.length; i++) 
 
     elements[i].style.background = '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 class="ra1">10:15-11:00AM</td> 
 
     <td style="background:black; color:Yellow;">It is Sunday </td> 
 

 
     </tr> 
 
     <tr> 
 
     <td class="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 class="ra1">10:15-11:00AM</td> 
 
     <td style="background:black; color:Yellow;">It is Monday</td> 
 

 
     </tr> 
 
     <tr> 
 
     <td class="ra3">11:00-11:45AM</td> 
 
     <td style="background:black; color:Yellow;">It is Monday</td> 
 

 
     </tr> 
 
    </table> 
 
    </div> 
 
</div>

+0

是的,它現在工作,我整天都在撓頭。如何更改高亮行的字體顏色?我不能使用選取框樣式顯示突出顯示行的類列中的文本嗎?它會很酷,然後感謝解決方案。 –

+0

您可以使用element.style.color更改字體顏色。 – Dij

+0

並且我不能在高亮行中使用字幕效果?在選定的表格行中是否有這種效果的解決方案?如果可以完成的話,那對我的項目來說是完美的。 –