0
代码:表靶向类行
var table = document.getElementById('some-table');
var row = table.rows;
alert(row.length); // Returns number of rows
// Function to hide class on specific row when some action is performed
function someFunc(i) {
var elementToTarget = row[i].getElementsByClassName('some-class');
var otherElementToTarget = row[i].getElementsByClassName('some-other-class');
elementToTarget.style.display="none"; // Returns "elementToTarget.style
// is undefined"
otherElementToTarget.style.display="inline"; // Returns undefined
}
基本表格布局:
<table>
<tr>
<td>
<div class="some-class"></div>
<div class="some-other-class"></div>
</td>
</tr>
</table>
我想要做什么:当用户触发someFunc(),它隐藏了一些一流并显示一些其他类。但它只能隐藏某一行的某些类。不是所有的行。
我的问题:它告诉我该行中的元素是未定义的,我无法对它们执行任何操作。我很可能是不恰当地瞄准这些类。
希望我已经清楚了。如果你需要更多的澄清,让我知道。
编辑:行中可能并不总是存在“某些其他类”。这就是为什么我不能只针对其中一个div的原因。我需要知道他们的行。
哪里是'someFunc()'调用,什么参数? – CodingIntrigue
@Rraham在这个例子中,它将在其他一些类的div上。像这样,onclick = someFunc(0)(因为它的第一行)。但是,如果它在第53行,它会是一些功能(52)。 –