簡體   English   中英

突出顯示懸停時的搜索關鍵字

[英]highlight search keywords on hover

我有一個基本的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;
}

我嘗試通過highlight:hover更改顏色,但這僅在將鼠標懸停在關鍵字本身上時才更改搜索關鍵字的顏色,這是可以理解的,因為這是應該起作用的方式,但是我希望搜索關鍵字能夠當我將鼠標懸停在整個結果上時,將突出顯示。

嘗試使用此CSS。 當您將鼠標懸停在行上時,它將使用“ highlight”類更新任何內容。 只需將此處的樣式更新為您想要在懸停效果上看到的樣式即可。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM