简单的javascript查找和替换 [英] Simple javascript find and replace

查看:80
本文介绍了简单的javascript查找和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可在div中搜索特定字符串并将其替换为另一个字符串?我不能单独使用.replaceWith,因为div中需要保留其他元素.我尝试了各种在这里找不到的javascript方法.

is there a straightforward method for searching within a div for a specific string and replacing it with another? I cannot use .replaceWith alone because there are other elements within the div I need to preserve. I've tried various javascript methods found here to no avail.

所以像这样:

$('#foo').find('this string').replaceWith('this other string');

针对:

<div id="foo"><div id="child">Other Element</div>this string</div>

谢谢.

推荐答案

尝试一下:

var foo = $('#foo').html();

foo = foo.replace('this string', 'this other string');

$('#foo').html(foo);

提琴: http://jsfiddle.net/maniator/w9GzF/

这篇关于简单的javascript查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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