【问题标题】:Get the latest caret position among multiple contenteditable div and append image to the div where the cursor was present lastly获取多个 contenteditable div 中的最新插入符号位置,并将图像附加到光标最后出现的 div
【发布时间】:2018-10-27 04:27:19
【问题描述】:

我需要根据最近编辑的事实在多个内容可编辑的 div 中附加一个图像。此外,contenteditable div 本身包含 HTML(不仅仅是文本),因此这些 div 中可能存在多个节点。为了更正确地理解这个问题,这里是link to jsfiddle

$('#img-div').on('click',function(){
  //logic for appending according to latest caret position

  //if caret wasnt in any content editable div then do this
  $('#div-4').append($(this).html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col" id="img-div">
      <img src="https://creww.io/assets/images/creww-coin.png">
    </div>
    <div class="col">
       <div contenteditable="true" id="div-1">
          Div 1 here
       </div>
       <div contenteditable="true" id="div-2">
          Div 2 here
       </div>
       <div contenteditable="true" id="div-3">
          Div 3 here
       </div>
       <div contenteditable="true" id="div-4">
          Div 4 here
       </div>
    </div>
  </div>
</div>

【问题讨论】:

标签: javascript jquery dom contenteditable caret


【解决方案1】:

你可以在最后出席mouseover活动。

$('#img-div').on('click',function(){
 

  //if caret wasnt in any content editable div then do this
  lastDiv.append($(this).html());
  //$('#divApp').append(lastDiv.html());
 
});

 //logic for appending according to latest caret position
var lastDiv;
$('div[id*="div-"]').mouseover(function() {
lastDiv =$( this );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col" id="img-div">
      <img src="https://creww.io/assets/images/creww-coin.png">
    </div>
    <div class="col">
       <div contenteditable="true" id="div-1">
          Div 1 here
       </div>
       <div contenteditable="true" id="div-2">
          Div 2 here
       </div>
       <div contenteditable="true" id="div-3">
          Div 3 here
       </div>
       <div contenteditable="true" id="div-4">
          Div 4 here
       </div>
    </div>
    <div id="divApp"></div>
  </div>
</div>

【讨论】:

  • 这不是我想要实现的。但是,通过使用您的函数,我可以找到鼠标悬停的最后一个 div,但我也想不从最后一个位置添加 div,而是从上次光标出现在该 div 中的任何位置追加 div
  • @AchalGupta 我有更新代码,所以上次在这个 div 中出现了光标。你可以在我的代码中找到。
  • 我很愿意,但我的问题没有完全解决。我想将图像插入上次光标所在的位置,就像我们通过拖放在邮件中插入图像一样(插入在闪烁的光标旁边)我想通过点击图像来实现同样的效果
  • @AchalGupta 你能解释一下做了什么和需要做什么吗?
  • 让我们把它分成两个问题:1)我想在我的光标最后定位的地方插入一个可内容编辑的 div 中的图像。类似于从 gmail 撰写邮件时插入图像的方式。图像被插入到光标所在的任何位置。(contenteditable div 中包含标签,而不仅仅是文本)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
相关资源
最近更新 更多