在一些JQuery对象的html中替换

时间:2012-09-16 04:45:48

标签: jquery replace

如果我有一些JQuery对象$("#someDiv"),其html(),

$("#someDiv").html(); 

等于:

<div id="bla_i">
  <input ... id="ble_i" />
</div>

(即:很多嵌套元素,其中一些有id和名称包含“_i”)

如何让所有“_i”成为“_5”? 如何替换某些JQuery对象中的所有“_i”?

1 个答案:

答案 0 :(得分:1)

var html = $('#someDiv').html();  // get the html string

$(html)  // make jQuery object
       .find('[id*="_i"]')  // find elements with id contains "_i"
       .attr('id',function(i, oldId) {
          return oldId.replace('_i', '_5');  // replace all "_i" with "_5"
       });