简体   繁体   English

使用JavaScript动态添加数据时,如何在可编辑内容的div文本末尾获得插入符号位置?

[英]How to get the caret position at the end of the text of a contenteditable div when the data is added dynamically using Javascript?

Note: Please, before marking it as duplicate, understand that I am adding the data dynamically using keypress function in Javascript. 注意:请在将其标记为重复之前,了解我正在使用Javascript中的keypress函数动态添加数据。

I am trying to create a script that adds data dynamically to a contenteditable div, I am able to do that with the following code. 我正在尝试创建一个脚本,该脚本将数据动态添加到一个内容可编辑的div中,我可以使用以下代码来实现。

   var enterPressed = 0;
   window.onkeypress = function (e) {
       var keyCode = (e.keyCode || e.which);

       if (keyCode === 13) {
           if (enterPressed === 0) {
               e.preventDefault();
               var z = document.createElement('p'); // is a node
               z.innerHTML = "<br><p>R: ";
               document.getElementById("textbox").appendChild(z);
               enterPressed++;
           } else if (enterPressed === 1) {
               e.preventDefault();
               var z = document.createElement('p'); // is a node
               z.innerHTML = "<br><b>M: ";
               document.getElementById("textbox").appendChild(z);
               enterPressed++;
               enterPressed = 0;

           }
       }
   };

So when enter is press once I get M: and if enter is press twice, I get R: and then the function reset. 因此,当按回车键一次时,我得到M:;如果按回车键两次时,我得到R :,然后功能复位。

The problem is that whenever I press enter the caret position still remains at the beginning of the document while ideally, it should be at the end so I can type something further. 问题在于,每当我按Enter键时,插入符号位置仍会保留在文档的开头,而理想情况下,它应位于文档的末尾,以便我可以进一步键入内容。

在此处输入图片说明

You can use this function to move the cursor to the end. 您可以使用此功能将光标移到末尾。

function setEndOfContenteditable(contentEditableElement) {
  var range,selection;
  if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
  {
      range = document.createRange();//Create a range (a range is a like the selection but invisible)
      range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
      range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
      selection = window.getSelection();//get the selection object (allows you to change selection)
      selection.removeAllRanges();//remove any selections already made
      selection.addRange(range);//make the range you have just created the visible selection
  }
  else if(document.selection)//IE 8 and lower
  { 
      range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
      range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
      range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
      range.select();//Select the range (make it the visible selection
  }
}

You just need to use this function when you are appending new child like this. 这样添加新的孩子时,只需要使用此功能即可。

let child = document.getElementById("textbox").appendChild(z);
setEndOfContenteditable(child)

Here is the full working code 这是完整的工作代码

  function setEndOfContenteditable(contentEditableElement) { var range,selection; if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+ { range = document.createRange();//Create a range (a range is a like the selection but invisible) range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start selection = window.getSelection();//get the selection object (allows you to change selection) selection.removeAllRanges();//remove any selections already made selection.addRange(range);//make the range you have just created the visible selection } else if(document.selection)//IE 8 and lower { range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible) range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start range.select();//Select the range (make it the visible selection } } var enterPressed = 0; window.onkeypress = function (e) { var keyCode = (e.keyCode || e.which); if (keyCode === 13) { if (enterPressed === 0) { e.preventDefault(); var z = document.createElement('p'); // is a node z.innerHTML = "<br><p>R: "; let child = document.getElementById("textbox").appendChild(z); setEndOfContenteditable(child) enterPressed++; } else if (enterPressed === 1) { e.preventDefault(); var z = document.createElement('p'); // is a node z.innerHTML = "<br><b>M: "; let child = document.getElementById("textbox").appendChild(z); setEndOfContenteditable(child) enterPressed++; enterPressed = 0; } } }; var elem = document.getElementById("textbox"); setEndOfContenteditable(elem) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <div contenteditable="true" id="textbox">Please press enter</div> </body> </html> 

You can check this post for details https://stackoverflow.com/a/3866442/5146848 您可以查看此帖子以获取详细信息https://stackoverflow.com/a/3866442/5146848

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将插入号/光标位置设置为可编辑div的末尾 - setting the caret/cursor position to the end of a contenteditable div 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? 更改可信的div文本时如何保留插入位置? - How do I preserve the caret position when changing a contenteditable div's text? javascript:将插入符号移动到元素的结尾(div与contenteditable) - javascript: move caret to end of element (div with contenteditable) 如何获得包含图像的contenteditable div的插入位置 - how to get the caret position of a contenteditable div which contains images 如何在 contentEditable div 中获取插入符号 x 和 y 位置 - How to get caret x and y position in contentEditable div 如何使用 html 子元素在 contenteditable div 中获取插入符号 position? - How to get caret position within contenteditable div with html child elements? React:编辑 contentEditable div 时如何保持插入符号位置? - React: How to maintain caret position when editing contentEditable div? 输入新行时,如何获取插入符号在contentEditable div中的位置? - How to get the caret's position in contentEditable div when entering a new line? h如何获得 contentEditable div 的插入符号位置? - hHow to get caret position for contentEditable div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM