Jquery替换html中的文本

替换html元素中的文本是否有更优雅的方式:

$el = $("#myDiv"); $el.html( $el.html().replace("something", "something new") ); 

 $('#myDiv').html(function(index, oldhtml) { return oldhtml.replace('something', 'something new'); }); 

如果我明白:

 oldText = $('#myDiv').html(); $('#myDiv').html( oldText.replace('something','something new') ); 

http://jsfiddle.net/wmgBN/

没有什么比你写的例子更优雅了! 查看这个例子http://blog.chapagain.com.np/jquery-how-to-replace-string-div-content-and-image-src/

你可以试试

 e1.html(function(i,oldHtml){ return oldhtml.replace("something", "something new"); } 

它的不同但不确定优雅。