javascript - How to apply a class to a specified grouping of characters in a string? -
trying reduce html in document applying class specific group of characters. in example group of asterisks, appear in document ** , code wrapped in
<span class="asterisk">**</span>
but there on 50 instances on page. looking way have class added dynamically defined group, in case **
thus far efficient solution has been suggested was
<script> $("span:contains('**')").addclass("asterisk"); </script>
which reduces amount of code needed , brings <span>**</span>
in example below if wanted '**' indicate food items , given class do
<span>**</span>indicates food related word <p>intelligentsia high life shoreditch keffiyeh skateboard, hoodie master cleanse helvetica. fanny pack <span>**</span>raw denim street art, wes anderson gentrify lomo lo-fi. </p> <p>selfies typewriter haven't heard of them <span>**</span>small batch street art, cliche raw denim try-hard sartorial cardigan pitchfork. forage flannel portland, small batch before sold out +1 post-ironic <span>**</span>pbr skateboard mixtape fap try-hard banjo bicycle rights. vinyl banksy banjo, <span>**</span>gastropub <span>**</span>four loko art party <span>**</span>food truck next level.</p>
see how can repetitive? intent type in '**' next want selected. , class dynamically added defined grouping of characters.
<p>**chambray letterpress **kale **chips **high life 90's wolf yr semiotics mcsweeney's biodiesel, flannel fixie retro.</p> <p>meh wayfarers fap scenester retro single-origin **coffee, cliche craft **beer 8-bit lo-fi selvage yr mixtape 90's.</p>
could use narrowing down go here finish up.
the clearest way use .addclass
specify class name(s) want add:
$("span:contains('**')").addclass("asterisk");
addclass
can accept function, advanced use, when want treat each element in jquery set differently. since want same thing each element, .addclass
not need function.
Comments
Post a Comment