Change last letter color

后端 未结 8 1464
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 16:42

Example code:

string

I want to change the color on the last letter, in this case \"g\", but I need soluti

相关标签:
8条回答
  • 2020-11-27 17:19

    Another solution is to use ::after

    .test::after{
        content: "g";
        color: yellow;
    }
    <p class="test">strin</p>
    

    This solution allows to change the color of all characters not only letters like the answer from Spudley that uses ::first-letter. See ::first-letter specification for more information. ::first-letter applies only on letters it ignores punctuation symbols.

    Moreover if you want to color more than the last character you can :

    .test::after{
         content: "ing";
         color: yellow;
    }
    <p class="test">str</p>
    

    For more information on ::after check this link.

    0 讨论(0)
  • 2020-11-27 17:21

    $(document).ready(function() {
      var str=$("span").text();
      strArr=str.split("");
      for(var key=0;key<=strArr.length-1;key++) {
        if(key==strArr.length-1) {
          var newEle="<span id='lastElement'>"+strArr[key]+"</div>";
          strArr[key]=newEle;
        }
      }
      var newtext=strArr.join("");
      $("span").html(newtext);  
    });
      
    span#lastElement {
      color: red;
    }

    0 讨论(0)
提交回复
热议问题