JQuery替换< p>中的文本部分与< img> [英] JQuery replace portion of text in <p> with an <img>

查看:706
本文介绍了JQuery替换< p>中的文本部分与< img>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的身体中有此代码:

 < p& 
hello ok hello no
< p>

< p>
ok hello ok ko
< p>

我要替换每个< p> 文本okby < img> 标签< img src =ok.pngalt =good word/> ;



注意:我必须申请对于可以包含撇号和/或引号的其他文本,此函数

解决方案

使用 $ c>:

  $('p')。each(function(){
this.innerHTML = this .innerHTML.replace(/ \bok\b / g,'< img src =ok.pngalt =good word/>')
})

我使用 / \bok\b / code>令牌将不匹配。 \b 表示字边界。



这是一个小提琴


I have this code in my body :

<p>
    hello ok hello no
<p>

<p>
    ok hello ok ko 
<p>    

I want to replace for each <p> the text 'ok' by <img> tag <img src="ok.png" alt="good word"/>

Is it possible to do this ?

Note : i must apply this function for others texts who can contain apostrophe and/or quote

解决方案

Use each:

$('p').each(function() {
    this.innerHTML = this.innerHTML.replace(/\bok\b/g, '<img src="ok.png" alt="good word"/>')
})

I used /\bok\b/ so that something like token won't match. \b means "word boundary."

Here's a fiddle.

这篇关于JQuery替换&lt; p&gt;中的文本部分与&lt; img&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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