簡體   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