替换div中的文本,而不会影响其中的任何HTML标记 [英] Replace text inside a div without affecting any HTML tags inside of it

查看:118
本文介绍了替换div中的文本,而不会影响其中的任何HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题已经问了很多遍了.但是,不幸的是,我还没有遇到直接的解决方案.大多数解决方案都围绕div中的多个节点进行.

I see that this has been asked many times. But, unfortunately I have not come across a straight forward solution. Most solutions revolve around multiple nodes within the div.

所以这里有问题.我有以下标记:

So here's problem. I have the following markup:

<div class="test">Text1<span></span></div>

我需要在不影响span标签和附加到span标签的事件处理程序的情况下,将"Text1"替换为"Text2".

I need "Text1" to be replaced with "Text2" without affecting the span tag and event handlers attached to the span tag.

执行类似$('.test')html('Text2<span></span>')的操作确实会替换文本.但是,删除了span标签上的事件处理程序,这是不希望的.我正在寻找一种快速有效的方法.

Doing something like $('.test')html('Text2<span></span>') does replace the text. But, removes the event handlers on the span tag, which is not desired. I am looking for a quick and efficient method for this one.

推荐答案

用标签包裹可替换文本:

Wrap replaceable text with a tag:

<div class="test"><span class="test-text">Text1</span><span></span></div>

这篇关于替换div中的文本,而不会影响其中的任何HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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