根据文本内容设置HTML表格单元格背景色

时间:2018-06-26 01:25:50

标签: html css perl html-table

我已经编写了一个Perl程序来创建一个具有从文本文件textfile.txt派生的HTML表的网页。

我想更改它,以便表格的单元格根据文本内容着色。例如,如果文本为Reject,则单元格的背景应为红色。

这是我尝试过的两种方法。他们俩都没有工作

方法1

if ( $_ eq "REJECT" ) {
    print map { "<td style=width:705 bgcolor=#FF0000 >REJECT</td>" } @$d;
}

方法2

foreach my $d ( @data ) {

    $d //= '';    # Convert undefined values to empty strings

    my $class;

    if ( $d eq 'REJECT' ) {
        $class = 'hilite';
    }

    $html .= '<td';
    $html .= " class='$class'" if $class;
    $html .= ">$d</td>";
}

Perl程序

#!/usr/bin/perl

print "Content-type: text/html\n\n";

use strict;
use warnings;

my $output = `cat textfile.txt`;
my @lines = split /\n/, $output;

my @data;

foreach my $line ( @lines ) {
    chomp $line;
    my @d = split /\s+/, $line;
    push @data, \@d;
}

my $color1 = "black";
my $color2 = "darkgreen";
my $color3 = "black";
my $color4 = "red";
my $color5 = "lime";

my $num    = 6;
my $title  = "This is the heading";
my $fstyle = "Helvetica";

print "<body bgcolor = $color3>";
print "<font color = $color5  face = $fstyle  size = $num>$title</font><br />";

foreach my $d ( @data ) {

    print "<html>";
    print "<body>";
    print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>";
    print "<tr>";
    print map {"<th style=width:705 >Column1</th>"}
            print map {"<th style=width:705 >Column2</th>"}
            print "</tr>";
    print "<tr>";
    print map {"<td style=width:705 >$_</td>"} @$d;

    if ( $d eq 'REJECT' ) {
        print map {"<td style=width:705 bgcolor=#FF0000 >Reject</td>"} @$d;
    }

    print "</tr>";
    print "</table>";
    print "</body>";
    print "</html>";
}

输入文本文件:

Column1 Column2
Accept   Reject
Accept   Reject
Accept   Reject

此行

print map { "<td style=width:705 bgcolor=#FF0000 >Reject</td>"

正在将背景色RED添加到单元格中,但不符合条件Reject

输出

enter image description here

2 个答案:

答案 0 :(得分:0)

这是您的Perl代码中的一些错误

  • 正如我所说,您滥用map

  • 您正在为@data的每个元素创建一个新的HTML文档。您希望浏览器如何处理多个<html>元素?它无法全部显示

  • 您期望字符串REJECTReject匹配

  • 您正在使用CSS style字符串和元素属性的混合体。例如

    print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>"
    

    应该是

    print qq{<table
            style="table-layout:fixed; width=705; height=110; text=$color4"
            border=2
            bordercolor="$color1"
            bgcolor="$color2">\n}
    

    因为table-layoutwidthheighttext是CSS属性,而borderbordercolor和{{1} }是HTML元素属性

    我认为您应该编写CSS来解决此问题,但这是另一回事

如果在每个HTML元素之后打印换行符bgcolor,这也将对您有很大帮助。这样,输出将更具可读性,您将能够更好地看到自己创建的内容

请不要坚持使用“尝试一切,直到一切正常” 方法。您总是总是来这里寻求帮助,以帮助您摆脱已创建的混乱局面,而且您也不会问聪明的问题。如此长时间使用"\n"意味着您根本不学习,而应归功于自己和您的雇主,以正确地学习语言

答案 1 :(得分:0)

这是一种可以正确执行的解决方案,但这仅是对您自己的代码的更正。我概述的问题已经修复,仅此而已

#!/usr/bin/perl

use strict;
use warnings 'all';

my $color1 = 'black';
my $color2 = 'darkgreen';
my $color3 = 'black';
my $color4 = 'red';
my $color5 = 'lime';

my $size   = 6;
my $title  = 'This is the heading';
my $fstyle = 'Helvetica';

print "Content-type: text/html\n\n";

print "<body bgcolor = $color3>\n";
print "<font color = $color5 face=$fstyle size=$size>$title</font><br />\n";

{
    print "<html>\n";
    print "<body>\n";

    print qq{<table
            style="table-layout:fixed; width=705; height=110; text=$color4"
            border=2
            bordercolor="$color1"
            bgcolor="$color2">\n};

    print "<tr>\n";
    print qq{<th style="width:705" >Column1</th>};
    print qq{<th style="width:705" >Column2</th>};
    print "</tr>\n";

    open my $fh, '<', 'textfile.txt' or die $!;

    while ( <$fh> ) {

        my @line = split;

        print "<tr>\n";

        for ( @line ) {

            if ( /reject/i ) {
                print qq{<td style=width:705 bgcolor=red>$_</td>};
            }
            else {
                print "<td style=width:705>$_</td>"
            }
        }

        print "</tr>\n";
    }

    print "</table>\n";
    print "</body>\n";
    print "</html>\n";
}

输出

Content-type: text/html

<body bgcolor = black>
<font color = lime face=Helvetica size=6>This is the heading</font><br />
<html>
<body>
<table
        style="table-layout:fixed; width=705; height=110; text=red"
        border=2
        bordercolor="black"
        bgcolor="darkgreen">
<tr>
<th style="width:705" >Column1</th><th style="width:705" >Column2</th></tr>
<tr>
<td style=width:705>Column1</td><td style=width:705>Column2</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
</table>
</body>
</html>

外观

This is the result

我仍然对您的做法感到担忧。从您不了解的其他零碎工作中窃取一个程序是失败的秘诀。如果您不愿意探索和学习足以独自生存的细节,那么您选择了错误的工作

  • 我认为您应该使用模板系统,例如 Template::Toolkit而不是从您的Perl程序中打印HTML

  • 应该使用CSS和适当的class更改颜色,而不是在行中打印HTML属性

您似乎认为休闲和近似的方法不错,或者至少您不愿意提供更多,但是尽管其他职业可能如此,但是软件工程需要更多的关注和精确度