CSS nth-child在类更改后不刷新

huangapple go评论98阅读模式
英文:

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(&quot;table-search&quot;).addEventListener(&#39;input&#39;, updatetable, false);


var table = document.getElementById(&quot;data-table-body&quot;);
var select = document.getElementById(&quot;tablefilter&quot;);

function updatetable()
{
    var search = &#39;&#39;;
    search = document.getElementById(&quot;table-search&quot;).value;
    search = search.toLowerCase();
    console.log(search + &quot;\n&quot; + select.value);

    var rows  = table.getElementsByTagName(&quot;tr&quot;);

    var row;
    var txtvalue = &#39;&#39;;

    for (var i in rows)
    {
        row = rows[i];
        
        if (select.value == &#39;name&#39;) {
            txtvalue = row.getElementsByTagName(&quot;th&quot;)[0].textContent || row.getElementsByTagName(&quot;th&quot;)[0].innerText;
            console.log(txtvalue);
            if (txtvalue.toLowerCase().includes(search)) {
                if (row.classList.contains(&quot;table-row-hide&quot;)) {
                    row.classList.remove(&quot;table-row-hide&quot;);
                    row.classList.add(&quot;table-row-show&quot;);
                }
            }
            else
            {
                if (row.classList.contains(&quot;table-row-show&quot;)) {
                    row.classList.remove(&quot;table-row-show&quot;);
                    row.classList.add(&quot;table-row-hide&quot;);
                }
            }
        }
    }
}

<!-- 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).

huangapple
  • 本文由 发表于 2023年3月7日 17:42:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定