如何用jquery定位特定的字母/单词? [英] How to target specific letter/word with jquery?

查看:116
本文介绍了如何用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 "&amp;" in the document.

The CSS:

.fancy { font-style: italic; }

So a text that looks like this:

Ben &amp; Jerry's

would be manipulated by jquery to this:

Ben <span class="fancy">&amp;</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('&amp;','<span class=\'fancy\'>&amp;</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屋!

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