简体   繁体   English

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

[英]how to get font colour or text colour of td value using javascript or jquery

I'm getting values of td if it contains "searched string" using 我正在获取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);
}

It is working fine. 一切正常。 Now I want to add one more condition. 现在,我想再添加一个条件。 How will I check for font colour of that. 我将如何检查字体颜色。 If it is equal to red then to proceed.Kindly help. 如果它等于红色,请继续进行。请提供帮助。 If that can't be done then help me in searching for "Error" now how will I check using criteria "Match whole word" . 如果不能这样做,请立即帮助我搜索“错误”,然后使用标准“匹配整个单词”进行检查 Search only for that particular string if any td has. 如果有任何td,则仅搜索该特定字符串。 If any td contains "Errorrr" it shouldn't consider that. 如果任何td包含“ Errorrr”,则不应考虑。

Check it... think, this is what yo need 检查一下...想,这就是您所需要的

 $('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> 

Here it is without JQuery, using getComputedStyle() . 在这里,它没有使用getComputedStyle() JQuery。 With this function you can get the actual style that is, after all the cascades and overwriting is done, rendered on the page: 使用此功能,您可以获得在所有级联和覆盖完成之后呈现在页面上的实际样式:

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);

This way you can get any actual, rendered CSS attribute from any element. 这样,您可以从任何元素获取任何实际的呈现的CSS属性。 The only caveat is to replace the kebab-case with camelCase ( background-color -> backgroundColor ). 唯一的警告是用camelCase( background-color color-> backgroundColor )替换kebab-case。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM