使用jquery仅替换div中的文本 [英] Replace only text inside a div using jquery
本文介绍了使用jquery仅替换div中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像:
<div id="one">
<div class="first"></div>
"Hi I am text"
<div class="second"></div>
<div class="third"></div>
</div>
我试图仅将我是文本中的文字更改为我好我更换 使用jquery。
这可能很容易,但我无法做到。
I am trying to change only the text from "Hi I am text" to "Hi I am replace" using jquery. This might be easy but I am not able to do it.
使用 $('#one')。text(' ')
只清空整个 #One
div。
Using $('#one').text('')
just empties the whole #One
div.
推荐答案
文本不应单独使用。将它放入 span
元素。
Text shouldn't be on its own. Put it into a span
element.
将其更改为:
<div id="one">
<div class="first"></div>
<span>"Hi I am text"</span>
<div class="second"></div>
<div class="third"></div>
</div>
$('#one span').text('Hi I am replace');
这篇关于使用jquery仅替换div中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文