首页 > 解决方案 > 如果数组包含innerHTML,则更改单元格背景颜色

问题描述

我正在尝试更改那些 innerHTML 值与我的数组匹配的单元格背景颜色。我怎样才能做到?

var numbers = ["15628","15623","15656","11628"];

var table = document.querySelector("[name='main']")
.contentWindow.document.getElementById("music").rows.length;

for (i = 4; i < table -1; i++){
    if (document.querySelector("[name='main']")
.contentWindow.document.getElementById("music").rows[i].cells[1].innerHTML == numbers)
    {
    document.querySelector("[name='main']")
.contentWindow.document.getElementById("music")
.rows[i].cells[1].style.backgroundColor = "yellow";
    };

标签: javascriptgoogle-chrome-extension

解决方案


你确定这个元素选择是正确的: document.querySelector("[name='main']").contentWindow.document.getElementById("music").rows[i].cells[1] ?不看 html 就很难分析。如果没问题,也许你需要这样的解决方案:

for (i = 4; i < table -1; i++){
    let value = document.querySelector("[name='main']")
.contentWindow.document.getElementById("music").rows[i].cells[1].innerHTML;

    if (numbers.includes(value))
    {
    document.querySelector("[name='main']")
.contentWindow.document.getElementById("music")
.rows[i].cells[1].style.backgroundColor = "yellow";
    };

推荐阅读