jQuery换行发现跨度内的字符串? [英] jquery wrap found string inside of a span?

查看:83
本文介绍了jQuery换行发现跨度内的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在所有h3的内部查找诸如"New"之类的特定字符串,并将其包装在

How do I look inside of all h3's for a specific string like "New" and wrap that inside of a

<span class="red">New </span>

推荐答案

使用 :contains()选择器在每个<h3>中找到单词New,然后在回调中使用 .html() String.replace()以应用<span>:

Use the :contains() selector to find the word New in every <h3>, then use .html() with a callback to String.replace() to apply the <span>:

$('h3:contains(New)').html(function() {
    return $(this).html().replace('New', '<span class="red">New</span>');
});

这变成了

<h3>New stuff</h3>

进入

<h3><span class="red">New</span> stuff</h3>

jsFiddle演示

这篇关于jQuery换行发现跨度内的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆