아래에서 코드를 수정하여 사용자가 열 머리글 상자를 클릭했을 때 열 머리글의 이름을 반환 할 수있는 방법은 무엇입니까? 예. "파일 번호"상자를 클릭하면 자바 스크립트 경고 상자에서 열 머리글의 이름 ("th)이"파일 번호 "라는 것을 알려줍니다.클릭 할 때 테이블 헤더 이름을 경고하십시오.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#mstrTable {
border: 1px solid black
}
#mstrTable td, th {
border: 1px solid black
}
#mstrTable tr.normal td {
color: black;
background-color: white;
}
#mstrTable tr.highlighted td {
color: white;
background-color: gray;
}
</style>
</head>
<body>
<table id="mstrTable">
<thead>
<tr>
<th>File Number</th>
<th>Date1</th>
<th>Date2</th>
<th>Status</th>
<th>Num.</th>
</tr>
</thead>
<tbody>
<tr>
<td>KABC</td>
<td>09/12/2002</td>
<td>09/12/2002</td>
<td>Submitted</td>
<td>0</td>
</tr>
<tr>
<td>KCBS</td>
<td>09/11/2002</td>
<td>09/11/2002</td>
<td>Approved</td>
<td>1 </td>
</tr>
<tr>
<td>WFLA</td>
<td>09/11/2002</td>
<td>09/11/2002</td>
<td>Submitted</td>
<td>2</td>
</tr>
<tr>
<td>WTSP</td>
<td>09/15/2002</td>
<td>09/15/2002</td>
<td>In-Progress</td>
<td>3</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
(
function()
{
var trows = document.getElementById("mstrTable").rows;
for (var t = 1; t < trows.length; ++t)
{
trow = trows[t];
trow.className = "normal";
trow.onclick = highlightRow;
}
function highlightRow(e)
{
alert('Row is ' + (this.rowIndex-1))
for (var t = 1; t < trows.length; ++t)
{
trow = trows[t];
trow.className = (trow == this && trow.className != "highlighted") ? "highlighted" : "normal";
}
}
}
)();
</script>
</body>
</html>
jQuery에 대해 살펴 보았습니까? 이것은 이러한 일을 단순하게 만듭니다. –
라이브러리 및 추가 파일을로드하지 않는 것이 좋습니다. – user1959234