ある要素内の文字列を置換する

var el = $('#id');
el.html(el.html().replace(/word/ig, ""));

http://www.catswhocode.com/blog/8-awesome-jquery-tips-and-tricks

この例酷すぎるでしょ。
置換対象の文字列がHTMLのタグ名に含まれるものならHTMLの構造が壊れるし、
属性の値と一致してた場合等も壊滅的におかしくなる。


やるならこうでしょ。textnodeのみ対象にすべき。

$('#id').find('*').andSelf()
    .contents().not('[nodeType=1]')
        .each(function(){
            this.nodeValue = this.nodeValue.replace(/word/ig,'');
        });

2009/05/11追記

迎賓館離宮
にて、IEで動かない件が指摘されていました。
textContentではなくnodeValueを使用するべきでした。