jQuery在整个div块中搜索字符串

时间:2014-12-28 21:37:27

标签: javascript jquery html

我有一个带有各种标签的代码块,但都在一组div标签中。我需要能够搜索div块中的所有内容以将字符串替换为另一个字符串。这是块。

<div class="form-group field-thoughts-cmh0-thought required">
<label class="control-label" for="thoughts-cmh0-thought">cmh0.</label>
<input type="text" id="thoughts-cmh0-thought" class="form-control" name="Thoughts[cmh0][thought]">
<button type="button" class="entAdd"> Add Above </button> 
<button type="button" class="entDelete"> Delete </button>
<div class="help-block"></div>
</div>

我要做的是用另一个字符串替换所有'cmh0'实例。我知道我可以一个接一个地做到这一点,但随着时间的推移,上面的块很可能会多次改变和改变。因此,我想要一种优雅的方法来选择和替换主div块中的所有'cmh0'实例。

我该怎么做?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以通过快速选择和替换来执行此操作:

var container = $('div[class*="cmh0"]').parent();
container.html(container.html().replace(/cmh0/g, 'someotherclass'));

class*=读为,&#34;选择类contanis&#34;`的任何元素。

然后只需替换html并重新应用它。

jsFiddle