首页 文章

如何使用javascript或jquery获取td值的字体颜色或文本颜色

提问于
浏览
0

如果它包含“搜索字符串”,我将获得td的值

var t1=$(this).find('tr:has(td:first-child:contains("Error"))');
alert($(this).find('tr:has(td:first-child:contains("Error"))').css === "red"));
if (t1.length) {
    str =t1.text().trim();
    str = /:(.+)/.exec(str)[1];
    errorArray.push(str);
    // alert(str);
}

它工作正常 . 现在我想再增加一个条件 . 我该如何检查它的字体颜色 . 如果它等于红色则继续 . 求助 . 如果无法完成,请帮助我搜索"Error"现在如何使用条件 "Match whole word" 进行检查 . 如果有任何td,则仅搜索该特定字符串 . 如果任何td包含"Errorrr",则不应该考虑这一点 .

2 回答

  • 0

    检查一下......想想,这就是你需要的东西

    $('table tr td').on('click',function(){
    alert($(this).css('background-color'));
    })
    
    table tr td{ border:solid 1px; padding:2px}
    table tr td:nth-child(even){ background-color:#ff3}
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <tr>
        <td>first</td>
        <td>second</td>
        <td>third</td>
        <td>fourth</td>
      </tr>
    </table>
    
  • 2

    这里没有JQuery,使用 getComputedStyle() . 使用此功能,您可以获得实际样式,即在完成所有级联和覆盖之后,在页面上呈现:

    const td = document.querySelector("table tr td"); // you can use JQuery here, if that makes you happy, or whichever way to select the element from the DOM.
    const colour = getComputedStyle(td).backgroundColor;
    
    console.log(colour);
    

    这样,您可以从任何元素获取任何实际的渲染CSS属性 . 唯一需要注意的是用camelCase替换烤肉串( background-color - > backgroundColor ) .

相关问题