繁体   English   中英

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

[英]Set HTML table cell background colour according to text content

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

我想对其进行更改,以使表格的单元格根据文本内容着色。 例如,如果文本为“ Reject则单元格的背景应为红色。

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

方法1

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

方法二

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不匹配。

输出量

在此处输入图片说明

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

  • 正如我所说,您滥用map

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

  • 您期望字符串REJECT匹配Reject

  • 您正在混合使用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属性,而borderbordercolorbgcolor是HTML元素属性

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

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

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

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

#!/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>

出现

这是结果

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

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

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

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

暂无
暂无

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

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