Sli*_*yyy 0 css jquery replace
我的HTML结构:
<div id="test-listing">
<article>
<a>Some URL</a><h3>Some Text</h3>
<p>Drink this coffee</p>
</article>
<article><a>Some URL</a><h3>Some Text</h3><p>Drink this Coffee</p></article>
<article><a>Some URL</a><h3>Some Text</h3><p>Drink this Coffeeland</p></article>
<article><a>Some URL</a><h3>Some Text</h3><p>Drink this coffees</p></article>
</div>
Run Code Online (Sandbox Code Playgroud)
我的CSS课程:
.hit-list {
background: none repeat scroll 0 0 rgba(255, 0, 0, 0.1);
border-bottom: 1px solid #FF0000;
}
Run Code Online (Sandbox Code Playgroud)
无论区分大小写,我都需要搜索文本咖啡.请注意,咖啡的文字也能像咖啡,咖啡,咖啡,Coffeeland,并将其应用特定的类.因此得到的代码将是
<div id="test-listing">
<article><a>Some URL</a><h3>Some Text</h3
<p>Drink this <span class="hit-list">coffee</span></p>
</article>
<article><a>Some URL</a><h3>Some Text</h3
<p>Drink this <span class="hit-list">Coffee</span></p>
</article>
<article><a>Some URL</a><h3>Some Text</h3
<p>Drink this <span class="hit-list">Coffee</span>land</p>
</article>
<article><a>Some URL</a><h3>Some Text</h3
<p>Drink this <span class="hit-list">coffee</span>s</p>
</article>
</div>
Run Code Online (Sandbox Code Playgroud)
我搜索了相关的帖子,但没有找到任何东西,因为我正在处理循环内容.此外,我不需要一个jQuery插件来实现这一目标.
谢谢.
你可以这样做:
$('div#test-listing article').html(function () {
return $(this).html().replace(/coffee(?=[^>]*<)/ig, "<span class='hit-list'>$&</span>");
});
Run Code Online (Sandbox Code Playgroud)
如果搜索字词是动态的,您可以执行以下操作:
var term = 'coffee';
$('div#test-listing article').html(function () {
return $(this).html().replace(new RegExp(term + "(?=[^>]*<)","ig"), "<span class='hit-list'>$&</span>");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2030 次 |
| 最近记录: |