jquery – 显示最后插入的html元素的Contenteditable

前端之家收集整理的这篇文章主要介绍了jquery – 显示最后插入的html元素的Contenteditable前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@我使用一个contenteditable div作为输入字段来键入一些文本,并通过该文本中的按钮(小html图片)插入图标.

只要文本比可满足的领域更窄,它就没问题了.

一旦文本比字段长(因此它部分隐藏):

当我输入一个文本字符时,一切都很好,按下键后会自动显示最后一个字符,这样你就可以随时看到你输入的内容了.
但是当我通过按钮输入一个图标时,图标就可以了,但它是隐藏的,因为字段内容不会移动以使新输入的图标可见,直到我输入另一个文本字符.

对此的任何解决方案,以便输入的最后一个元素(文本或html)始终对用户可见?

  1. function pasteIcon(html) {
  2. var sel,range;
  3. if (window.getSelection) {
  4. sel = window.getSelection();
  5. if (sel.getRangeAt && sel.rangeCount) {
  6. range = sel.getRangeAt(0);
  7. range.deleteContents();
  8. var el = document.createElement("div");
  9. el.innerHTML = html;
  10. var frag = document.createDocumentFragment(),node,lastNode;
  11. while ((node = el.firstChild)) {
  12. lastNode = frag.appendChild(node);
  13. }
  14. range.insertNode(frag);
  15. if (lastNode) {
  16. range = range.cloneRange();
  17. range.setStartAfter(lastNode);
  18. range.collapse(true);
  19. sel.removeAllRanges();
  20. sel.addRange(range);
  21. }
  22. }
  23. } else if (document.selection && document.selection.type != "Control") {
  24. document.selection.createRange().pasteIcon(html);
  25. }
  26. }
  27.  
  28. $(document).ready(function() {
  29. $('.buttOn').click(function() {
  30. $('.contEd').focus();
  31. pasteIcon('<img class="icOn" src="http://www.bmgstuff.com/files/interface/generator_frame/text_blood.png">');
  32. })
  33. });
  1. [contenteditable="true"] {
  2. display: inline;
  3. white-space: nowrap;
  4. overflow: hidden !important;
  5. text-overflow: inherit;
  6. -webkit-user-select: text !important;
  7. -moz-user-select: text !important;
  8. -ms-user-select: text !important;
  9. user-select: text !important;
  10. }
  11.  
  12. [contenteditable="true"] br {
  13. display: none;
  14. }
  15.  
  16. .contAiner {
  17. display: flex;
  18. }
  19.  
  20. .buttOn {
  21. width: 24px;
  22. height: 24px;
  23. border: none;
  24. background: #666;
  25. color: white;
  26. }
  27.  
  28. .contEd {
  29. height: 22px;
  30. text-align: center;
  31. width: 100px;
  32. line-height: 23px;
  33. color: black;
  34. font-size: 10.5px;
  35. font-family: arial;
  36. border: 1px solid black;
  37. }
  38.  
  39. .icOn {
  40. width: 9px;
  41. height: 13px;
  42. top: 1px;
  43. position: relative;
  44. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <div class="contAiner">
  3. <input class="buttOn" type="button" value="B">
  4. <div class="contEd" contenteditable="true" spellcheck="false" autocomplete="off"></div>
  5. </div>

这是jsFiddle

这是original thread我从中获取了“pasteIcon”功能.

PS:我试图触发一个键码39(右箭头),就在pasteIcon函数之后,为了模拟一个按键,但它只是不起作用.

解决方法

您只需将编辑器滚动到插入的图标即可.移动选择后,请注意两行代码.希望它按预期工作:)

更新:

为了涵盖所有情况,我们需要检查插入的图像是否在编辑器边界内或外.
首先,让我们在编辑器元素中添加id,以便更容易找到它.然后我们可以利用函数getBoundingClientRect在屏幕上返回一个实际元素的矩形.最后,我们比较矩形,如果图像矩形不在编辑器内(imgRect.left< editorRect.left || imgRect.right> editorRect.right),那么我们滚动.

更新2:

在调查最新评论中描述的问题时,我发现在经过一定长度的编辑内容后,jQuery函数’offset’会返回不准确的结果.最有可能的原因是,编辑器的leftOffset在这种情况下不会自动更新.
最后,我将所需的滚动位置计算更改为图像DOM元素的offsetLeft减去编辑元素的offsetLeft减1(边框大小),现在它可以正常使用任何内容长度.

  1. function pasteIcon(html) {
  2. var sel,lastNode;
  3. while ((node = el.firstChild)) {
  4. lastNode = frag.appendChild(node);
  5. }
  6. range.insertNode(frag);
  7. if (lastNode) {
  8. range = range.cloneRange();
  9. range.setStartAfter(lastNode);
  10. range.collapse(true);
  11. sel.removeAllRanges();
  12. sel.addRange(range);
  13.  
  14. var editorRect = $(contEdit)[0].getBoundingClientRect();
  15. var imgRect = $(lastNode)[0].getBoundingClientRect();
  16. if (imgRect.left < editorRect.left || imgRect.right > editorRect.right) {
  17. var actualLeft = $(lastNode)[0].offsetLeft - editorRect.left - 1;
  18. $(".contEd").scrollLeft(actualLeft);
  19. }
  20. }
  21. }
  22. } else if (document.selection && document.selection.type != "Control") {
  23. document.selection.createRange().pasteIcon(html);
  24. }
  25. }
  26.  
  27. $(document).ready(function() {
  28. $('.buttOn').click(function() {
  29. $('.contEd').focus();
  30. pasteIcon('<img class="icOn" src="http://www.bmgstuff.com/files/interface/generator_frame/text_blood.png">');
  31. })
  32. });
  1. [contenteditable="true"] {
  2. display: inline;
  3. white-space: nowrap;
  4. overflow: hidden !important;
  5. text-overflow: inherit;
  6. -webkit-user-select: text !important;
  7. -moz-user-select: text !important;
  8. -ms-user-select: text !important;
  9. user-select: text !important;
  10. }
  11.  
  12. [contenteditable="true"] br {
  13. display: none;
  14. }
  15.  
  16. .contAiner {
  17. display: flex;
  18. }
  19.  
  20. .buttOn {
  21. width: 24px;
  22. height: 24px;
  23. border: none;
  24. background: #666;
  25. color: white;
  26. }
  27.  
  28. .contEd {
  29. height: 22px;
  30. text-align: center;
  31. width: 100px;
  32. line-height: 23px;
  33. color: black;
  34. font-size: 10.5px;
  35. font-family: arial;
  36. border: 1px solid black;
  37. }
  38.  
  39. .icOn {
  40. width: 9px;
  41. height: 13px;
  42. top: 1px;
  43. position: relative;
  44. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <div class="contAiner">
  3. <input class="buttOn" type="button" value="B">
  4. <div id="contEdit" class="contEd" contenteditable="true" spellcheck="false" autocomplete="off"></div>
  5. </div>

猜你在找的jQuery相关文章