Change color of a character

前端 未结 4 426
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 04:21

I have the following HTML code:

相关标签:
4条回答
  • 2020-12-18 04:24

    I think I would solve it CSS wise:

    label:first-letter {
    
        color: #ff0000;
    
    }
    

    Example here

    0 讨论(0)
  • 2020-12-18 04:28

    If you insist on a scripted solution:

    $("p > label:contains('*')").each(function () {
        $(this).html($(this).html().replace("*", "<span class='red'>*</span>"));
    });
    

    Demo.

    0 讨论(0)
  • 2020-12-18 04:38

    Karim's answer is great, but when implementing it, I noticed it will only select one character within every

    tag, so in this case:

    <p>
      <label>* Name</label>
      <label>* Last Name</label>
      <label>Mascot Name</label>
    </p>
    

    it wont't work. No problem if you replace the selector with "label:contains('*')" in this case, but what if you want to change the color (or other properties) of every instance of that specific character? So in this case:

    <p>
      * Name<br/>
      * Last Name<br/>
      Mascot Name
    </p>
    

    you really need something else (I know it doesn't look to good, but this is actual output from Wordpress). I found this: Change formatting of a specific character using css or jquery, where Blazemonger's answer does the trick! I'll repeat it here:

    $('selector').html(function(i,el) {
      return el.replace(/\*/g, '<span class="color">*</span>');
    });​
    
    0 讨论(0)
  • 2020-12-18 04:47

    You should rather use css to make this happen. Use the following instead

    <label><span style="color:red">*</span> Name</label>
    

    This will do the work.

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