Jquery replacing text inside html
Is there a more elegant way replacing a text inside an html element than :开发者_如何学C
$el = $("#myDiv");
$el.html( $el.html().replace("something", "something new") );
$('#myDiv').html(function(index, oldhtml) {
return oldhtml.replace('something', 'something new');
});
If I understand:
oldText = $('#myDiv').html();
$('#myDiv').html( oldText.replace('something','something new') );
http://jsfiddle.net/wmgBN/
there is nothing more elegant than the example you wrote! Check this exmple http://blog.chapagain.com.np/jquery-how-to-replace-string-div-content-and-image-src/
you can try
e1.html(function(i,oldHtml){
return oldhtml.replace("something", "something new");
}
its different but not sure about elegance.
精彩评论