如何用jquery定位特定的字母/单词? [英] How to target specific letter/word with jquery?
问题描述
例如,我想将花哨
类应用于符号的所有匹配 & amp;
.fancy {font-style:italic; }
这样一个文本看起来像这样:
Ben& amp; Jerry的
会被jquery操作到:
Ben< span class =fancy>& amp;< / span> Jerry's
有这样的功能来指定特定的字词/短语/字母吗?
这不是jQuery通常有用的 - 它在节点级别比text / html级别更有效。但是,这可能有助于( source ):
$('p:contains(&)')。 each(function(){
$(this).html(
$(this).html()。replace('& amp;','< span class = \'fancy\\ \\'>& amp;< / span>')
);
});
显然,如果你可以将初始搜索限制在比所有段落更好的地方,它会更好。您还应该测试它,看看:包含
过滤器是否实际上有帮助。
这不漂亮,工作。
As a mere example, I want to apply the class "fancy
" to all occurrences of the sign "&
" in the document.
The CSS:
.fancy { font-style: italic; }
So a text that looks like this:
Ben & Jerry's
would be manipulated by jquery to this:
Ben <span class="fancy">&</span> Jerry's
Is there a function to target specific words/phrases/letters like this?
This isn't something that jQuery is generally helpful with--it works more at the node level than the text/html level. However, this might help (source):
$('p:contains(&)').each(function(){
$(this).html(
$(this).html().replace('&','<span class=\'fancy\'>&</span>')
);
});
Obviously if you can restrict the initial search to something better than all paragraphs, it'd perform better. You should also test it to see if the :contains
filter actually helps.
It's not pretty but it seems to work.
这篇关于如何用jquery定位特定的字母/单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!