私はこれを使用しています:
$(body_doc).find('body').bind('paste',function(e){
var rte = $(this);
_activeRTEData = $(rte).html();
beginLen = $.trim($(rte).html()).length;
setTimeout(function(){
var text = $(rte).html();
var newLen = $.trim(text).length;
//identify the first char that changed to determine caret location
caret = 0;
for(i=0;i < newLen; i++){
if(_activeRTEData[i] != text[i]){
caret = i-1;
break;
}
}
var origText = text.slice(0,caret);
var newText = text.slice(caret, newLen - beginLen + caret + 4);
var tailText = text.slice(newLen - beginLen + caret + 4, newLen);
var newText = newText.replace(/(.*(?:endif-->))|([ ]?<[^>]*>[ ]?)|( )|([^}]*})/g,'');
newText = newText.replace(/[·]/g,'');
$(rte).html(origText + newText + tailText);
$(rte).contents().last().focus();
},100);
});
body_docは編集可能なiframeです。編集可能なdivを使用している場合は、.find('body')部分を削除できます。基本的に、貼り付けイベントを検出し、場所をチェックして新しいテキストをクリーンアップしてから、クリーンアップしたテキストを貼り付けた場所に戻します。(紛らわしいように聞こえます...しかし、それは思ったほど悪くはありません。
setTimeoutが必要なのは、テキストが実際に要素に貼り付けられるまでテキストを取得できないためです。貼り付けが開始されるとすぐに貼り付けイベントが発生します。