英文:
CSS nth-child not refreshing after class change
问题
以下是您提供的代码的翻译部分:
// 监听表格搜索框的输入事件
document.getElementById("table-search").addEventListener('input', updatetable, false);
var table = document.getElementById("data-table-body");
var select = document.getElementById("tablefilter");
function updatetable() {
var search = '';
search = document.getElementById("table-search").value;
search = search.toLowerCase();
console.log(search + "\n" + select.value);
var rows = table.getElementsByTagName("tr");
var row;
var txtvalue = '';
for (var i in rows) {
row = rows[i];
if (select.value == 'name') {
txtvalue = row.getElementsByTagName("th")[0].textContent || row.getElementsByTagName("th")[0].innerText;
console.log(txtvalue);
if (txtvalue.toLowerCase().includes(search)) {
if (row.classList.contains("table-row-hide")) {
row.classList.remove("table-row-hide");
row.classList.add("table-row-show");
}
}
else {
if (row.classList.contains("table-row-show")) {
row.classList.remove("table-row-show");
row.classList.add("table-row-hide");
}
}
}
}
}
/* CSS样式 */
.table-row-show:nth-child(odd) {
@apply bg-white dark:bg-gray-900;
}
.table-row-show:nth-child(even) {
@apply bg-gray-50 dark:bg-gray-800;
}
.table-row-hide {
display: none;
}
请注意,这是您提供的代码的中文翻译部分。如果您需要进一步的协助,请随时提出。
英文:
I have a table with alternate background colored rows. I use a very simple javascript for table filtering. I am using tailwind css.
Now when I change the classes using javascript, it updates the visibility of rows as it was expected but it doesn't update the background color of the rows.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.getElementById("table-search").addEventListener('input', updatetable, false);
var table = document.getElementById("data-table-body");
var select = document.getElementById("tablefilter");
function updatetable()
{
var search = '';
search = document.getElementById("table-search").value;
search = search.toLowerCase();
console.log(search + "\n" + select.value);
var rows = table.getElementsByTagName("tr");
var row;
var txtvalue = '';
for (var i in rows)
{
row = rows[i];
if (select.value == 'name') {
txtvalue = row.getElementsByTagName("th")[0].textContent || row.getElementsByTagName("th")[0].innerText;
console.log(txtvalue);
if (txtvalue.toLowerCase().includes(search)) {
if (row.classList.contains("table-row-hide")) {
row.classList.remove("table-row-hide");
row.classList.add("table-row-show");
}
}
else
{
if (row.classList.contains("table-row-show")) {
row.classList.remove("table-row-show");
row.classList.add("table-row-hide");
}
}
}
}
}
<!-- language: lang-css -->
.table-row-show:nth-child(odd)
{
@apply bg-white dark:bg-gray-900;
}
.table-row-show:nth-child(even)
{
@apply bg-gray-50 dark:bg-gray-800;
}
.table-row-hide
{
display: none;
}
<!-- end snippet -->
答案1
得分: 1
将元素设置为 display: none
会停止它渲染,但不会将其从DOM中移除。
包含三行的表体仍然包含三行。如果第二行的 display: none
,则第三行仍然是第三行。
不能使用 nth-child
来选择基于其他元素是否可见的元素。您需要使用其他方法(例如,应用于每个可见行的类名)。
英文:
Setting an element to display: none
stops it from rendering. It does not remove it from the DOM.
A table body containing three rows contains three rows. If the second row is display: none
then the third row is still the third row.
You can't use nth-child
to select elements based on what other elements are visible. You'd need to use some other approach (such as class names applied to every visible row).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论