How to customize a specific letter / word using jquery?

As a simple example, I want to apply the class fancy"to all occurrences of the character" &"in the document.

CSS:

.fancy { font-style: italic; }

So the text is as follows:

Ben & Jerry's

will be processed by jquery as follows:

Ben <span class="fancy">&amp;</span> Jerry's

Is there a function to target specific words / phrases / letters like this?

+4
source share
3 answers

This is not what jQuery usually helps - it works more at the node level than at the text / html level. However, this may help ( source ):

$('p:contains(&)').each(function(){
  $(this).html(
    $(this).html().replace('&amp;','<span class=\'fancy\'>&amp;</span>')
  );
});

, - , , . , :contains .

, , .

+4

All Articles