我有一個鏈接,顯示/隱藏一個div內的內容點擊後,我想要做的是有一個按鈕,將隱藏一個特定的div(sectiontohide1中的內容)在div部分隱藏)點擊。我已經得到了JavaScript來實現第一個鏈接(sectiontohide)工作,它的工作原理,但我無法弄清楚如何獲得第二個鏈接隱藏在divOnclick隱藏div元素
<a id="trackAttendance1" onclick="loadtable('sectiontohide');" class="trackAttendance" href="#">Track Absences</a>
<div id="sectiontohide" style="display:none;">
<table>
<div id="sectiontohide1">
<tr>
<td>Web Programming Seminar</td>
<th id="absent" rowspan="2">Absent</th>
</tr>
<tr>
<td>TUESDAY 2:00-3:00 - 21/02/2017</td>
</tr>
</div>
<td>Management in IT</td>
<th id="absent" rowspan="2">Absent</th>
<tr>
<td>FRIDAY 9:00-11:00 - 24/02/2017</td>
</tr>
<tr>
<td>Web Programming Lecture</td>
<th id="absent" rowspan="2">Absent</th>
</tr>
<tr>
<td>FRIDAY 12:00-1:00 - 24/02/2017</td>
</tr>
</table>
<a id="trackAttendance1" onclick="hidediv('sectiontohide1');" class="close" href="#">Hide Element</a>
我的JavaScript用於顯示/隱藏表格
function loadtable(id){
var divelement = document.getElementById(id);
if(divelement.style.display =='none')
divelement.style.display = 'block';
else
divelement.style.display = 'none';
}
好吧,如果你有第一個工作,那麼肯定第二個是相同的。替換你的「onclick =」hidediv('sectiontohide1');「to」loadtable('sectiontohide1');「 –
一方面注意:一個ID只能在HTML文檔中使用,因此,替換id =」absent「例如,你的元素上有一個類('class =「absent」'),你也可以使用數據屬性 – floriangosse
'div#sectiontohide1'是不允許的,因爲你只能使用有效的表格元素作爲子元素一張桌子 – floriangosse