用jQuery包装一些指定的单词

用jQuery包装一些指定的单词

本文介绍了用jQuery包装一些指定的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例:
我有一个单词,它是是狗。原文如下:

 我有一条狗,你有狗吗? 

输出应该是这样的:

<$我有一个< span class =highlight>狗< / span>,你有< span class =highlight>狗< / span> ;?

任何帮助?

编辑:
请注意,它也应该包装单词,即使它们不是单个(彼得狗)。希望你明白了。

解决方案

这是一个有用的。

它的使用非常简单它的效果很好。



edit —这是我的一个非常古老的答案。我很遗憾,只是简单地链接了Lettering.js库而没有包含它的名字。图书馆 很有用,尽管7年后我不确定Rupert先生是否继续积极维护它。



什么是Lettering将穿过DOM的选定区域中的文本节点并将单个字母和/或单词(可配置)包裹在< span> 标签中,然后可以对其进行样式化。这很简单但很有效。即使你不想使用它,代码也不是很广泛,而且是教学。


I'd like to know the most convenient way to wrap some specified words with span-tags.

Example:I have a word, which is dog. Here's the original text:

I have a dog, do you have a dog?

And output should be like this:

I have a <span class="highlight">dog</span>, do you have a <span class="highlight">dog</span>?

Any help?

EDIT:Note, that it should also wrap words, even if they are not invidual (Peter-dog). Hope you got it.

解决方案

Here's a useful jQuery plugin.

It's really easy to use and it works great.

edit — this is a very old answer of mine. I regret having simply linked the Lettering.js library without including its name. The library is useful, though I'm not sure here 7 years later whether Mr. Rupert continues to actively maintain it.

What Lettering does is go through the text nodes in a selected region of the DOM and wrap the individual letters and/or words (configurable) in <span> tags that can then be styled. It's simple but effective. Even if you don't want to use it, the code is not very extensive and it's instructional.

这篇关于用jQuery包装一些指定的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:04