我正在努力实现项目,我必须在JavaScript中做一个WYSIWYG编辑器。我不能使用现有的编辑器,因为我需要使用我的插件(例如colorPicker或imagePicker)。
现在我有了这个HTML:
<div class="K_editor" id="idExample">
<div class="K_links">
<div class="K_editor_link K_editor_linkBold">B</div>
<div class="K_editor_link K_editor_linkItalic">I</div>
<div class="K_editor_link K_editor_linkUnderline">U</div>
</div>
<iframe width="696" height="212" frameborder="0" src="js/myEditor_iFrame.php">
<html>
<head/>
<body>
<div id="contentIframe" contenteditable="true">
This is a test code, with <strong>bold</strong> text and <em>italic</em> text.
</div>
</body>
</html>
</iframe>
<input type="submit"/>
</div>在.K_editor_link上单击事件时,函数将使用参数打开:
tagStart (示例<u>或<span style="color:#AB1;">)tagEnd (例如</u> )或</span>)id (此处为idExample))
我知道在Textarea上有选择,但setSelectionRange()、.selectionStart和.selectionEnd只适用于textbox (XUL)、input (XHTML)或textarea (XHTML)。
我能做些什么呢?
发布于 2012-07-29 23:10:41
这是我使用的代码。因为几个月前我在这里发现了它,所以我不能把它归功于它。不记得在哪了。希望它对你有用。
function getSelectionHtml()
{
var html = "";
if (typeof window.getSelection != "undefined")
{
var sel = window.getSelection();
if (sel.rangeCount)
{
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}代码来自于这个问题:window.getSelection return html
https://stackoverflow.com/questions/2713270
复制相似问题