2015-12-09 70 views
0

我想将所选文本从一个内容div移动到另一个内容div。所选文本应附加在另一个div的当前插入位置。有没有办法根据div id获得护理职位?我在所有来源中观察到,获得的脱字符位置总是基于选择。通过单击按钮将选定的文本从一个内容div移动到另一个内容div

<body > 
<div contenteditable="false" id='selectfromhere' > 
    some text select 
    </div> 
    <div contenteditable="true" id='movehere' > 
    some 
    </div> 
    <button>Click me</button> 
</body> 

回答

0

这里是获取的内容编辑的插入位置的解决方案: Get caret position in contentEditable div

如果您使用该功能来获取插入符位置(VAR caretPosition)的整数值,下面应该插入文本:

var insertText = $("#selectfromhere").html(); 
var currentText = $("#movehere").html(); 
var output = [currentText.slice(0, caretPosition), insertText, currentText.slice(caretPosition)].join(''); 

$("#movehere").html() = output; 
+0

获取CONTENTEDITABLE格链接插入位置将返回selectfromhere格的插入位置。你可以让我知道,如何获得movehere div的插入位置,以便我可以将文本从选择区域移动到插入位置 – seetha

+0

在此函数内......“var update = function(){ $( '(#)“)。html(getCaretPosition(this)); }; $('#contentbox')。on(”mousedown mouseup keydown keyup“,update); ”您是否确定将#contentbox替换为# movehere而不是#selectfromhere –

2

试试这个:

var selectedText = ''; 
 

 
$(document).ready(function() { 
 
    $('#selectfromhere').mouseup(function() { 
 
    selectedText = window.getSelection().toString(); 
 
    console.log(selectedText); 
 
    }); 
 

 
    $('button').click(function() { 
 
    $('#selectfromhere').html($('#selectfromhere').html().split(selectedText).join("")); 
 
    $('#movehere').append(selectedText); 
 
\t \t window.getSelection().removeAllRanges(); 
 
    selectedText = ''; 
 
    console.log('Some Text ' + selectedText); 
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body> 
 
    <div contenteditable="false" id='selectfromhere'> 
 
    some text select 
 
    </div> 
 
    <div contenteditable="true" id='movehere'> 
 
    
 
    </div> 
 
    <button type="button">Click me</button> 
 
</body>

希望这回答了你的问题 /Zorken17

+0

我编辑了代码,现在您可以选择多次从第一个元素复制文本。如果这回答你的问题,请将其标记为一个。 – Zorken17

相关问题