在悬停时突出显示搜索关键字

时间:2010-05-28 15:57:55

标签: php css search highlight

我有一个基本的php搜索表单,使用css突出显示关键字。我想知道是否只有当用户将鼠标悬停在记录上时才能使结果中的关键字突出显示。这有可能吗?

这是重点代码:

    function highlightWords($text, $words) {
        preg_match_all('~\w+~', $words, $m);
        if(!$m)
            return $text;
        $re = '~\\b(' . implode('|', $m[0]) . ')~i';
        $string = preg_replace($re, '<span class="highlight">$0</span>', $text);

        return $string;
    }


. . .

<table class="result">
    <?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
        $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
        ?>
        <tr>
        <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
        <td style="font-size:16px;"><?php echo $cQuote; ?></td>
        <td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
        <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
        </tr>
    <?php } ?>
</table>

的CSS:

table.result tr:hover {
  background:#F7F7F7;
  }

.highlight {
font-weight:bold;
color: #DE2842;
padding:5px;
padding-right:2px;
background: #FFFCDB;
}

我尝试通过高亮显示更改颜色:悬停,但这只会在我悬停在关键字本身上时更改搜索关键字的颜色,这是可以理解的,因为这是它应该工作的方式,但我想要当我将鼠标悬停在整个结果上时,搜索要突出显示的关键字。

1 个答案:

答案 0 :(得分:1)

尝试使用此CSS。当您将鼠标悬停在该行上时,它将使用“突出显示”类更新任何内容。只需将此处的样式更新为您希望在悬停效果上看到的内容。

table.result tr:hover .highlight {
    color: #FFFFFF;
    background: #FF0000;
}