jquery - Find a string of text in an element and wrap some span tags round it -
i want find string of text in element , wrap span tags round it. eg:
from <h2>we have cows on our farm</h2> <h2>we have <span class='smallcaps'>cows</span> on our farm</h2>
i've tried:
$("h2:contains('cow')").each(function() { $(this).text().wrap("<span class='smallcaps'></span>"); }); but wraps whole containing h2 tag.
$("h2:contains('cow')").html(function(_, html) { return html.replace(/(cow)/g, '<span class="smallcaps">$1</span>'); });
Comments
Post a Comment