Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从div中获取突出显示的文本并复制到textareajavascript中_Javascript - Fatal编程技术网

从div中获取突出显示的文本并复制到textareajavascript中

从div中获取突出显示的文本并复制到textareajavascript中,javascript,Javascript,设法从一个文本区域中获取突出显示的文本,并将其传输到另一个文本区域。但是当代码被编辑以便从div中获取高亮显示的文本时,它就不起作用了 知道为什么会这样吗?谢谢 <div id="quote"> load transcript in here instead and grab text from here</div> // does not work <textarea id="quote" readonly> // works load transcrip

设法从一个文本区域中获取突出显示的文本,并将其传输到另一个文本区域。但是当代码被编辑以便从div中获取高亮显示的文本时,它就不起作用了

知道为什么会这样吗?谢谢

<div id="quote"> load transcript in here instead and grab text from here</div> // does not work

<textarea id="quote" readonly> // works
load transcript in here
</textarea>


<textarea contenteditable="false" id="output" name="selected"></textarea> // outputs highlighted text here


<script>

var quotearea = document.getElementById('quote')
var output = document.getElementById('output')
quotearea.addEventListener('mouseup', function(){
    if (this.selectionStart != this.selectionEnd){ // check the user has selected some text inside field
        var selectedtext = this.value.substring(this.selectionStart, this.selectionEnd)
        output.innerHTML = selectedtext
    }
}, false)

</script>
在此处加载成绩单并从此处抓取文本//不起作用
//工作
在这里加载成绩单
//在此处输出突出显示的文本
var quotearea=document.getElementById('quote')
var output=document.getElementById('output')
quotearea.addEventListener('mouseup',function(){
如果(this.selectionStart!=this.selectionEnd){//检查用户是否在字段内选择了一些文本
var selectedtext=this.value.substring(this.selectionStart,this.selectionEnd)
output.innerHTML=selectedtext
}
},错)
小提琴:


评论中有人用一把小提琴回答了这个问题,小提琴上有两个文本区域,但将其中一个编辑成了div,看起来很管用。干杯(编辑-结果表明不需要div,它直接从页面中获取,因此是一种解决方法)


把成绩单放在这里。
var text='';
函数copyText(e){
text=(document.all)?document.selection.createRange().text:document.getSelection();
document.getElementById('copied')。值=文本;
}
document.onmouseup=copyText;
如果(!document.all){
document.captureEvents(Event.MOUSEUP);
}
在这里演奏小提琴:


我在评论中回答了你的问题,并将其删除

您正在使用仅对输入元素有效的
selectionStart
selectionEnd
方法。对于您的解决方案,改为使用
document.selection.createRange().text
,它获取文档中选定的文本(输入、div等不重要)

这是小提琴:


你能为它做一把小提琴吗这可能会给你你所需要的:添加一把小提琴,现在检查链接,看起来很相关!干杯,真管用,杰勒多!!非常感谢。用这把最新的小提琴:谢谢你,伙计!这个解决方案非常有意义。非常感谢。仅供参考,fiddle中的代码只适用于整个文档,不使用特定的选择器,因此如果您只想在无法工作的div中使用它。是的,我注意到,如果有一种方法可以解决这一问题,使它从特定的选择器中获取,这将是很酷的,否则应该不会有太多问题。太好了。当然,这是可能的。您可以在此处搜索如何创建特定元素的selectionrange并获取其内容。如果你找不到任何东西,明天早上我会送你一把小提琴。干杯
<div name="" id="original">
Load transcript in here.
</div>

<textarea name="" id="copied" cols="30" rows="10"></textarea>

<script>


var text = '';
function copyText(e) {
    text = (document.all) ? document.selection.createRange().text : document.getSelection();
    document.getElementById('copied').value = text;
}
document.onmouseup = copyText;
if (!document.all) {
    document.captureEvents(Event.MOUSEUP);
}


</script>