自分でテキストエディタを作りたいのですが。私は他のいくつかの定評のあるエディターの例に従い、隠されたコンボ<textarea>
と目に見える<iframe>
コンボを使用しようとしています。
キー入力の最初のステップで問題が発生しました<iframe>
。execCommandをコンテンツ編集可能で動作させて <div>
、たとえばテキストを太字にすることができますが、で同じことを行うのに問題があり<iframe>
ます。
これが私のHTMLです:
<html>
<button type="button" class="btn btn-bold">bold</button>
<textarea id="input" name="input" style="display: none;">Start typing..</textarea>
<iframe frameborder="0" src="javascript:true;" style=""> </iframe>
</html>
これが私のJavaScriptです:
$(function() {
var currentBtn;
$("#input").focus(function() {
currentBtn = this;
});
$(".btn").on( 'click', '.btn-bold', function(){
var $this=$(this);
if($this.hasClass("btn-bold")){ document.execCommand('bold', false, null);}
$(currentBtn).contents().focus();
});
});
親ドキュメントではなくiframeドキュメントでexecCommandを実行する必要があることはわかっていますが、これを行う方法がわかりません。現在、このコードではiframeをキーアップできないため、太字のボタンの効果をテストできません。
どんな考えでも大歓迎です!