【发布时间】:2014-05-31 23:55:34
【问题描述】:
我即将开始编写自己的富文本编辑器,但需要知道是否可以填充文本区域以及如何保存/使用其中的数据。
我目前正在使用 CKEditor,但它对于我想要的东西来说太笨重而且太大了。
我将以此为基础:http://jsfiddle.net/Kxmaf/6/
我还需要对数据进行某些检查以检查其长度。
需要的代码:
// Stack Code Snippets does not work with Iframe. Here's the code anyways:
$(document).ready(function() {
document.getElementById('textEditor').contentWindow.document.designMode = "on";
document.getElementById('textEditor').contentWindow.document.close();
$("#bold").click(function() {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
} else {
$(this).addClass("selected");
}
boldIt();
});
$("#italic").click(function() {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
} else {
$(this).addClass("selected");
}
ItalicIt();
});
$("#fonts").change(function() {
changeFont($("#fonts").val());
});
});
function boldIt() {
var edit = document.getElementById("textEditor").contentWindow;
edit.focus();
edit.document.execCommand("bold", false, "");
edit.focus();
}
function ItalicIt() {
var edit = document.getElementById("textEditor").contentWindow;
edit.focus();
edit.document.execCommand("italic", false, "");
edit.focus();
}
function changeFont(font) {
var edit = document.getElementById("textEditor").contentWindow;
edit.focus();
edit.document.execCommand("FontName", false, font);
edit.focus();
}
<a id="bold" class="font-bold">B</a>
<a id="italic" class="italic">I</a>
<select id="fonts">
<option value="Arial">Arial</option>
<option value="Comic Sans MS">Comic Sans MS</option>
<option value="Courier New">Courier New</option>
<option value="Monotype Corsiva">Monotype</option>
<option value="Tahoma">Tahoma</option>
<option value="Times">Times</option>
</select>
<br/>
<iframe id="textEditor" style="width:500px; height:170px;"></iframe>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
谢谢。
【问题讨论】:
-
出于好奇...你为什么要使用 iframe ??
-
我在寻找如何制作富文本编辑器时遇到了这个问题。是否可以在普通文本区域使用 execCommand 等?
-
文本区域?你可以在 DIV 上做到这一点!
-
我明白了!我要有戏。谢谢!
-
我已经添加了一个演示,其中包含您所需要的只是......更简单。我仍然不明白的是在哪里以及为什么要保存编辑器数据。
标签: javascript jquery iframe designmode