dtcaw02086 2017-11-18 11:59
浏览 156
已采纳

在PHP表格单元格中为特定单词添加颜色

I want to color the word "Pizza" in class "status", which is inside my HTML table, which gets data from a database with PHP.

      <!-- populate table from mysql database -->
        <?php
        while($row=mysqli_fetch_array($records))
        {
            ?>
            <tr>
            <td class="status"><?php echo $row['pojem'];?></font></td>
            <td><font color="brown"><?php echo $row['kratica'];?></font></td>
            <td><font color="brown"><?php echo $row['nem'];?></font></td>
            </tr>
            <?php
        }
    ?>

I have tried the following but it doesn't work

<script>
$('.status:contains("Pizza")').css('color', 'red');
</script>
  • 写回答

2条回答 默认 最新

  • dsarttv037029 2017-11-18 12:20
    关注

    You may use the following jQuery code and set CSS class colord with the color or the style you want.

    $(document).ready(function(){
      $('td.status').each(function(){
       text = $(this).html();
       $(this).html(text.replace('Pizza','<span class="colord">Pizza</span>')); // html is used instead of text
    });
    
    })
    

    Edit

    The text method is replaced with html method to make void HTML escape in the string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部