如何使用jQuery:contains(some text)选择器,但只选择“this is some text”中的“some text”?
我有这样的事情:
<div class="search">this is some text</div>
我目前正在使用这样的代码:
$("div.search:contains(some text)").css("text-decoration","underline");
但是,这突出了“这是一些文字”,我希望它只是强调“一些文字”。
我怎样才能最简单地完成这项工作?
答案 0 :(得分:2)
您必须使用正则表达式将内部文本用自己的元素包装:
$('div.search:contains(some text)').html(function (i, v) {
return v.replace('some text', '<span>some text</span>');
})
.find('span').css('text-decoration', 'underline');
仅当div.search
中没有嵌套元素时才使用此选项。如果有,那么这是一场完全不同的球赛。
答案 1 :(得分:0)
你可以撕掉像jQuery高亮插件这样的东西来找到它们是如何做到的。或者您可以按原样使用插件,只需修改应用的css类以包含下划线而不是黄色背景颜色。
不一定回答这个问题,但是如果你想要的是功能而不是学习......不要重新发明轮子。