到目前为止,我有一个
<table>
有很多
<tr>
表行。每行中有两个单元格(
<td>
<td>
<tr>
<td class="codecol">
<input type="checkbox">
<a title="I&C SCI 33" onclick="return showCourse(this, 'I&C SCI 33');">I&C SCI 33</a>
</td>
<td>Intermediate Programming</td>
</tr>
但是,如果选中了复选框,我想删除链接和文本描述(如果未选中,则取消排序)。因此,我编写了一个JS函数,如下所示:
function checkStrikethrough(checkboxElemnt, rowData) {
//strikethrough text if checked
if (checkboxElemnt.checked) {
for(var i = 0; i < rowData.length ; i++) {
var elemnt = document.getElementById(rowData[i]);
elemnt.style.setProperty("text-decoration", "line-through");
}
}
// normal text if not checked
else {
for(var i = 0; i < rowData.length ; i++) {
var elemnt = document.getElementById(rowData[i]);
elemnt.style.setProperty("text-decoration", "none");
}
}
}
我遇到的问题是HTML文件中更改的硬编码:id作为参数传递。有这么多行,我开始怀疑是否有比这样修改更好的方法:
<tr>
<td class="codecol">
<input type="checkbox" onchange='checkStrikethrough(this, ["icsci33Link", "icsci33Td"]);'>
<a id="icsci33Link" title="I&C SCI 33" onclick="return showCourse(this, 'I&C SCI 33');">I&C SCI 33</a>
</td>
<td id="icsci33Td">Intermediate Programming</td>
</tr>
<tr>
<td class="codecol">
<input type="checkbox" onchange='checkStrikethrough(this, ["icsci45cLink", "icsci45cTd"]);'>
<a id="icsci45cLink" title="I&C SCI 45C" onclick="return showCourse(this, 'I&C SCI 45C');">I&C SCI 45C</a>
</td>
<td id="icsci45cTd">Programming in C/C++ as a Second Language</td>
</tr>