duanmao9918 2015-09-24 13:44
浏览 400
已采纳

更改字符串中每个单词的字体颜色(JS或PHP)

I've searched but cannot find a solution to increment the font color (eg. lightness +10%) for each of 3 or 4 words in a title. The font color will be initially set in SCSS with a color var.

Example Title:

Here Is My New Title

In this example, lets say the title was 'dark blue'.. the words would be:

Here = darker blue
Is = navy blue
My = medium blue
New Title = light blue

There is a similar post here: JavaScript Text Color Change To Each Word In Array but this is searching for keywords in an array, not each word of a string.

I've also come across basic CSS/HTML only solutions like this, which won't work: HTML: Changing colors of specific words in a string of text

NOTE: I will be returning the title (string) in a php variable - in case there is a solution in PHP.

GOAL: I need to increment the font color (or even wrap consecutive words in spans and increment the SCSS var) for each word in a string.. open to suggestions.

UPDATE I've added a jsfiddle link (http://jsfiddle.net/revive/xyy04u7d/) to show the JS implementation, thanks to Tushar, with some changes to include ampersands in the regex.

Here is the PHP implementation:

<?php
    $title = "SIMPLE TITLE & WITHOUT COLORS";
    $words   = preg_split('/\s+(?!&)/', $title);
?> 
<h3 class="colors blue">
<?php foreach($words as $word):?>
    <span><?php echo $word; ?></span>
<?php endforeach; ?>
</h3>
  • 写回答

3条回答 默认 最新

  • dpb56083 2015-09-24 14:00
    关注

    You can wrap the each word in separate span or any other element and then can be styled differently using the CSS nth-child property.

    Using this approach, you don't have to create separate classes, and it'll work for any number of words in string.

    var str = 'Split the Text by one or more spaces. Join them to make them wrap in span elements. Then use CSS nthChild Properties :)';
    
    str = '<span>' + str.split(/\s+/).join('</span> <span>') + '</span>';
    
    $(document.body).append(str);
    span:nth-child(4n) {
      color: red;
    }
    span:nth-child(4n + 1) {
      color: green;
    }
    span:nth-child(4n + 2) {
      color: blue;
    }
    span:nth-child(4n + 3) {
      color: gray;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>


    You can also use regex as follow:

    $("h1").html(function(i, oldHtml) {
      return oldHtml.replace(/(\S+)/g, '<span>$1</span>');
    });
    span:nth-child(4n) {
      color: red;
    }
    span:nth-child(4n + 1) {
      color: green;
    }
    span:nth-child(4n + 2) {
      color: blue;
    }
    span:nth-child(4n + 3) {
      color: gray;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <h1> Hello World! Have a Great Day! </h1>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系