Javascript 将插入符号位置移动到内容可编辑的焦点()后(<;部门>;
我试图用JavaScript创建一个非常基本的富文本编辑器,但我在选择方面遇到了问题。因此,基本上,由于它是一个可编辑的内容,任何时候用户从网页粘贴预先格式化的文本,格式不会被剥离 一个容易破解的方法是在按下Ctrl+V键时将焦点放在a上,这样文本就会粘贴到a上,然后按下键,将焦点放回a上,将内容复制到a上,并删除进入a的内容 所以这很容易,但是当我把焦点放回内容时&;Tdiv>,插入符号位置在开始位置,而不是粘贴后的立即位置。我对选择和其他方面的知识还不够了解,所以我希望能得到一些帮助。这是我的密码:Javascript 将插入符号位置移动到内容可编辑的焦点()后(<;部门>;,javascript,contenteditable,rich-text-editor,Javascript,Contenteditable,Rich Text Editor,我试图用JavaScript创建一个非常基本的富文本编辑器,但我在选择方面遇到了问题。因此,基本上,由于它是一个可编辑的内容,任何时候用户从网页粘贴预先格式化的文本,格式不会被剥离 一个容易破解的方法是在按下Ctrl+V键时将焦点放在a上,这样文本就会粘贴到a上,然后按下键,将焦点放回a上,将内容复制到a上,并删除进入a的内容 所以这很容易,但是当我把焦点放回内容时&;Tdiv>,插入符号位置在开始位置,而不是粘贴后的立即位置。我对选择和其他方面的知识还不够了解,所以我希望能得到一些帮助。这是我
// Helpers to keep track of the length of the thing we paste, the cursor position
// and a temporary random number so we can mark the position.
editor_stuff =
{
cursor_position: 0,
paste_length: 0,
temp_rand: 0,
}
// On key up (for the textarea).
document.getElementById("backup_editor").onkeyup = function()
{
var main_editor = document.getElementById("question_editor");
var backup_editor = document.getElementById("backup_editor");
var marker_position = main_editor.innerHTML.search(editor_stuff.temp_rand);
// Replace the "marker" with the .value of the <textarea>
main_editor.innerHTML = main_editor.innerHTML.replace(editor_stuff.temp_rand, backup_editor.value);
backup_editor.value = "";
main_editor.focus();
}
// On key down (for the contentEditable DIV).
document.getElementById("question_editor").onkeydown = function(event)
{
key = event;
// Grab control + V end handle paste so "plain text" is pasted and
// not formatted text. This is easy to break with Edit -> Paste or
// Right click -> Paste.
if
(
(key.keyCode == 86 || key.charCode == 86) && // "V".
(key.keyCode == 17 || key.charCode == 17 || key.ctrlKey) // "Ctrl"
)
{
// Create a random number marker at the place where we paste.
editor_stuff.temp_rand = Math.floor((Math.random() * 99999999));
document.getElementById("question_editor").textContent += editor_stuff.temp_rand;
document.getElementById("backup_editor").focus();
}
}
//帮助跟踪粘贴内容的长度,光标位置
//还有一个临时的随机数,这样我们可以标记位置。
编辑者
{
光标位置:0,
粘贴长度:0,
温度:0,
}
//按向上键(用于文本区域)。
document.getElementById(“备份编辑器”).onkeyup=function()
{
var main_editor=document.getElementById(“问题编辑器”);
var backup_editor=document.getElementById(“备份编辑器”);
var marker_position=main_editor.innerHTML.search(editor_stuff.temp_rand);
//将“标记”替换为的.value
main_editor.innerHTML=main_editor.innerHTML.replace(editor_stuff.temp_rand,backup_editor.value);
backup_editor.value=“”;
main_editor.focus();
}
//按下键时(对于contentEditable DIV)。
document.getElementById(“问题编辑器”).onkeydown=函数(事件)
{
关键=事件;
//抓取控件+V端手柄粘贴,以便粘贴“纯文本”并
//未格式化文本。使用编辑->粘贴或
//右键单击->粘贴。
如果
(
(key.keyCode==86 | | key.charCode==86)和&//“V”。
(key.keyCode==17 | | key.charCode==17 | | key.ctrlKey)/“Ctrl”
)
{
//在粘贴位置创建一个随机数字标记。
编辑器_stuff.temp_rand=Math.floor((Math.random()*9999999));
document.getElementById(“问题编辑器”).textContent+=editor\u stuff.temp\u rand;
document.getElementById(“备份编辑器”).focus();
}
}
因此,我的想法是将光标位置(整数)存储在助手数组中(editor\u stuff.cursor\u position
)
注意:我一整天都在看其他答案,但没有一个对我有用。这里有一个在插入符号位置插入文本的函数: 演示: 代码:
函数粘贴文本插入(文本){
var-sel,范围;
if(window.getSelection){
//IE9和非IE
sel=window.getSelection();
if(sel.getRangeAt&&sel.rangeCount){
范围=选择范围(0);
range.deleteContents();
var textNode=document.createTextNode(文本);
range.insertNode(textNode);
//保留所选内容
range=range.cloneRange();
range.setStartAfter(textNode);
范围。塌陷(真);
选择removeAllRanges();
选择添加范围(范围);
}
}else if(document.selection&&document.selection.type!=“控制”){
//IE<9
document.selection.createRange().text=text;
}
}
你好,蒂姆。我希望是你在看到所有关于其他选择相关问题的答案后做出回应。实际上,我将不再试图通过JavaScript控制用户输入,因为它很容易破坏可用的解决方案。不知道为什么他们不允许我们在粘贴时处理文本或允许“强制为纯文本”功能。我认为PHP是处理文本的更好方法。尽管如此,我仍然使用了您的代码,因为它是如何在编辑器中包含选项卡的一个很好的示例,这是我不久将要研究的内容。谢谢
function pasteTextAtCaret(text) {
var sel, range;
if (window.getSelection) {
// IE9 and non-IE
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
var textNode = document.createTextNode(text);
range.insertNode(textNode);
// Preserve the selection
range = range.cloneRange();
range.setStartAfter(textNode);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
} else if (document.selection && document.selection.type != "Control") {
// IE < 9
document.selection.createRange().text = text;
}
}