【问题标题】:looping through table rows循环遍历表行
【发布时间】:2011-07-19 22:03:31
【问题描述】:

我正在寻找遍历表格的行并检查它们的背景颜色。这是我目前所拥有的:

var TheColor = "";

$('#MyTable tr').each(function () {
    TheColor = "";
    TheColor = $(this).attr('background-color');
    alert(TheColor);
});

当循环展开时,我得到的只是“未定义”,我不明白为什么。但是,当我写 TheColor = $(this).html();我得到了预期的输出。

感谢您的建议。

【问题讨论】:

  • 你的“tr”真的有“背景颜色”属性吗?您可能应该查看具有该属性的样式属性。

标签: jquery css


【解决方案1】:

你需要像这样选择它:

$(this).css('background-color');

因为您要查找的属性实际上是一个样式属性 (style="background-color: red")。

【讨论】:

  • 确实如此。 background-color 不是tr 元素的属性,它是一条样式信息。 @frenchie:更多:api.jquery.com/css/#css1
  • 好的,这行得通。但是,它以 rgb(...) 的形式返回值。你知道为什么它没有返回 #FDFEGG 格式的数据吗?
【解决方案2】:
$('#MyTable tr').map(function(item){ return item.css('background-color'); })

这将返回一个背景颜色数组,数组中的每个元素都代表表格中的背景颜色。

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多