内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - Color every character differently - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Color every character differently - Stack Overflow

programmeradmin0浏览0评论

I am using KK Countdown to countdown to Xmas for a site.

I have a design which I have to follow that has each letter of the day countown with a blue background and border radius.

Right now the html is output like this

<span class="kkcount-down" data-time="1387929600">
     <span class="kkcountdown-box">
          <span class="kkc-dni">169</span>
          <span class="kkc-dni-text">DAYS </span>
          <span class="kkc-godz">23</span>
          <span class="kkc-godz-text"> </span>
          <span class="kkc-min">19</span>
          <span class="kkc-min-text"> </span>
          <span class="kkc-sec">48</span>
          <span class="kkc-sec-text">HOURS</span>
     </span>
</span>

The class kkc-dni is the part I am trying to target here.

I want to add a background colour to each letter inside that span.

Preferably with CSS. Is this possible?

I have used CSS before to style the first letter of paragraphs before but this is quite different and I cannot find any information on it.

Any suggestions?

Note: Because I am using a plugin to do this countdown I am not sure if I can change the way it outputs the spans and html. If I could wrap each letter in a span I would.

I am using KK Countdown to countdown to Xmas for a site.

I have a design which I have to follow that has each letter of the day countown with a blue background and border radius.

Right now the html is output like this

<span class="kkcount-down" data-time="1387929600">
     <span class="kkcountdown-box">
          <span class="kkc-dni">169</span>
          <span class="kkc-dni-text">DAYS </span>
          <span class="kkc-godz">23</span>
          <span class="kkc-godz-text"> </span>
          <span class="kkc-min">19</span>
          <span class="kkc-min-text"> </span>
          <span class="kkc-sec">48</span>
          <span class="kkc-sec-text">HOURS</span>
     </span>
</span>

The class kkc-dni is the part I am trying to target here.

I want to add a background colour to each letter inside that span.

Preferably with CSS. Is this possible?

I have used CSS before to style the first letter of paragraphs before but this is quite different and I cannot find any information on it.

Any suggestions?

Note: Because I am using a plugin to do this countdown I am not sure if I can change the way it outputs the spans and html. If I could wrap each letter in a span I would.

Share Improve this question edited Dec 18, 2014 at 5:40 Roko C. Buljan 207k41 gold badges327 silver badges337 bronze badges asked Jul 8, 2013 at 0:44 Chris G-JonesChris G-Jones 6264 gold badges14 silver badges27 bronze badges 5
  • I think that is not possible only with CSS. You have the :first-letter pseudo-element... But each letter, I think that isn't possible. – leoMestizo Commented Jul 8, 2013 at 0:49
  • you'd have to grab the content of the span jQuery('.kkc-dni').html(), then iterate over each letter String.split(""), then add each letter to its own span. to go further into explanation, would have to know more about how you want to color each letter – Marc Commented Jul 8, 2013 at 0:51
  • Thank you Marc, yes I thought that this would be possible with some jQuery. Was hoping that there would be a CSS selector for each character – Chris G-Jones Commented Jul 8, 2013 at 0:52
  • you could also do something clever if the font is fixed width. you could make a background image with the colors you need. – Marc Commented Jul 8, 2013 at 0:57
  • Thanks Marc, think I will do some jQuery and wrap each character. Feel like I have more control that way as opposed to an image. Good idea though! – Chris G-Jones Commented Jul 8, 2013 at 1:02
Add a ment  | 

3 Answers 3

Reset to default 4

I want to add a background colour to each letter inside that span.

Using an array of colors:

const colors = ["#0bf", "#f0b", "#fb0", "#b0f"];

$('.kkc').find('span').each(function() {

  const text = $(this).text();
  const len = text.length;
    
  const newCont = [...text].reduce((a, ch, i) => 
    a + `<span style="background:${colors[i % colors.length]}">${ch}</span>`, ""
  );

  $(this).html(newCont);

});
.kkcountdown-box>span>span {
  background: red;
}
<div class="kkc">
  <span class="kkc-dni">169</span>
  <span class="kkc-dni-text">DAYS </span>
  <span class="kkc-godz">23</span>
  <span class="kkc-godz-text"> </span>
  <span class="kkc-min">19</span>
  <span class="kkc-min-text"> </span>
  <span class="kkc-sec">48</span>
  <span class="kkc-sec-text">HOURS</span>
</div>

<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.5.1/jquery.min.js"></script>

The above will, wrapping every single letter into a separate span will also add a background color from your Array list of colors.
Once the colors list end is reached will start from the first one and so on.

Using a random color:

$('.kkc').find('span').each(function() {

  const text = $(this).text();
  const len = text.length;
    
  const newCont = [...text].reduce((a, ch, i) => {
    const color= '#'+ (Math.random() * 0xffffff).toString(16).substr(-6); 
    return a + `<span style="background:${color}">${ch}</span>`
  }, "");

  $(this).html(newCont);

});
.kkcountdown-box>span>span {
  background: red;
}
<div class="kkc">
  <span class="kkc-dni">169</span>
  <span class="kkc-dni-text">DAYS </span>
  <span class="kkc-godz">23</span>
  <span class="kkc-godz-text"> </span>
  <span class="kkc-min">19</span>
  <span class="kkc-min-text"> </span>
  <span class="kkc-sec">48</span>
  <span class="kkc-sec-text">HOURS</span>
</div>

<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.5.1/jquery.min.js"></script>

The above will get every letter inside a <span> and wrap it into a new span with a randomly generated background color.

You cannot style each letter of word without individually wrapping characters. This is the simple answer to the question, especially as asked in the the title.

However, the wrapping can be done with server-side scripting, or with client-side scripting. If this is acceptable, you should reformulate your question.

Assuming you aren't trying to give each letter a different background color, you can just specify CSS for each span like this:

.kkc-dni {background-color: red;}

Example here: http://jsfiddle/jfriend00/WAPds/

If you want each letter to have a different background color or other unique CSS, then you will have to wrap each letter in its own individual span. You can do that programmatically using JS if you can't control the generated HTML.

发布评论

评论列表(0)

  1. 暂无评论