【发布时间】:2011-05-02 06:29:56
【问题描述】:
我正在尝试编写一个函数,该函数允许内容可编辑的 div 在用户输入 div 时进行一些自动格式化。到目前为止,我只设法使它在 IE 中工作。谁能帮帮我?
function formatOnKeyUp(){
if (window.getSelection) {
// ???????
} else if (document.selection) {
cursorPos=document.selection.createRange().duplicate();
clickx = cursorPos.getBoundingClientRect().left;
clicky = cursorPos.getBoundingClientRect().top;
}
text = document.getElementById('div1').innerHTML;
text = text.replace(/this/gm, "<i>this</i>");
// .... some other formating here...
document.getElementById('div1').innerHTML = text;
if (window.getSelection) {
// ????????
} else if (document.selection) {
cursorPos = document.body.createTextRange();
cursorPos.moveToPoint(clickx, clicky);
cursorPos.select();
}
}
【问题讨论】:
-
为什么不使用现成的编辑器,让您可以根据自己的喜好设置文本格式?查看tinymce.moxiecode.com
-
我需要它自动格式化为用户类型,而不是用户点击某个按钮。此外,这是整个应用程序的一部分。第三方所见即所得编辑器不是我所追求的。
标签: javascript html contenteditable