我有一个div。例如,我想将 [1234] 的所有实例替换为 [5678],但仅限在 #groovy DIV 内,而不是在整个文档内。
我已经尝试过:
jQuery("#groovy").html().replace(/stuff\[1234\]/g,"stuff[5678]");
我对此没有任何运气。
<div id="groovy">
<h1>Some Stuff</h1>
<input name="stuff[1234][morestuff][otherstuff]">
<p>Things</p>
<input name="stuff[1234][goodstuff][betterstuff]">
<div class="telly">
<input name="stuff[1234][badstuff][decentstuff]">
</div>
</div>
最佳答案
$("#groovy input[name*='[1234]']").each(function(){
var item = $(this);
item.attr('name', item.attr('name').replace('[1234]', '[5678]'))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="groovy">
<h1>Some Stuff</h1>
<input name="stuff[1234][morestuff][otherstuff]">
<p>Things</p>
<input name="stuff[1234][goodstuff][betterstuff]">
<div class="telly">
<input name="stuff[1234][badstuff][decentstuff]">
</div>
</div>
关于javascript - 使用 JS 或 Jquery 替换特定 DIV 中嵌套键的所有实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59958959/