替换< p>中的单词在< div>使用jquery [英] Replace word in <p> in <div> using jquery
本文介绍了替换< p>中的单词在< div>使用jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件,其结构如下:
< div id =noticeclass =box generalbox >
< p>
这是一些文字。
< / p>
< / div>
我想用jQuery替换单词我的一词。
我如何做到这一点?
我试过:
<$ (#)。text()。replace(some,My); pre> $(#notice)。
但是没有效果...
更新:
感谢您的回复。我使用这个解决方案来实现它:
$(#notice p)。text($(#notice ())。text()。replace(some,My));
解决方案
阅读 http://api.jquery.com/text/#text-functionindex--text
$(#notice p)。text(function(_,ctx){
return ctx.replace(some,我的);
});
或
$(#notice p)。text($(#notice p)。text()。replace(some,My));
或
var p_tag = $(#notice p);
p_tag.text(p_tag.text()。replace(some,My));
I have a document with the following structure:
<div id="notice" class="box generalbox">
<p>
This is some text.
</p>
</div>
I want to replace the word "some" with the word "My" using jQuery.
How do i do this?
I tried:
$("#notice").text().replace("some", "My");
But that didnt work...
UPDATE: Thanks for all your replys. I used this solution to get this to work:
$("#notice p").text($("#notice p").text().replace("some", "My"));
解决方案
Read http://api.jquery.com/text/#text-functionindex--text
$("#notice p").text(function (_, ctx) {
return ctx.replace("some", "My");
});
or
$("#notice p").text($("#notice p").text().replace("some", "My"));
or
var p_tag = $("#notice p");
p_tag.text(p_tag.text().replace("some", "My"));
这篇关于替换< p>中的单词在< div>使用jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文