JS simple implementation of the element copy example attached

  • 2020-03-29 23:51:11
  • OfStack

The source code:
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<meta name="Author" content=""> 
<title> Content filtering </title> 

<script type='text/javascript'> 

function getPositions() { 
var el = document.getElementById('shaixuan'); 
var startPosition = 0;//The starting position of the selected text
var endPosition = 0;//The end of the selected text
if(document.selection) { 
//IE 
var range = document.selection.createRange();//Create scope objects
var drange = range.duplicate();//Clone object

drange.moveToElementText(el); //Copy the scope
drange.setEndPoint('EndToEnd', range); 

startPosition = drange.text.length - range.text.length; 
endPosition = startPosition + range.text.length; 
} 
else if(window.getSelection) { 
//Firefox,Chrome,Safari etc 
startPosition = el.selectionStart; 
endPosition = el.selectionEnd; 
} 
return { 
"start":startPosition, 
"end":endPosition 
} 
} 

//@todo gets the selected text in the textarea
function getshaixuan() { 
var position = getPositions(); 
var start = position.start;//The starting position
var end = position.end;//End position
var text = document.getElementById('shaixuan').value; 
var selectText = text.substr(start, (end - start));//Textarea, the selected text
//alert(selectText); 
var textBox1 = document.getElementById("canjia"); 
textBox1.innerText=textBox1.value+selectText; 
} 
//]]> 
</script> 

<title> Replication of elements </title> 
<style type="text/css"> 
<!-- 
body{ font-size:18px} 
p{border-style:none} 
.p1{ background-color:#FFFFFF;margin-top:60px;margin-bottom:3px;margin-left:200px;} 
.p2{ background-color:#FFFFFF;margin-top:2px;margin-bottom:3px;margin-left:150px;} 
.p3{ background-color:#FFFFFF;margin-top:1px;margin-bottom:3px;position:absolute;left:550px;top:60px} 
.p4{ background-color:#FFFFFF;margin-top:1px;margin-bottom:3px;position:absolute;left:500px;top:85px} 
.p5{ background-color:#FFFFFF;margin-top:1px;margin-bottom:3px;position:absolute;left:380px;top:190px} 
--> 
</style> 
</head> 

<body> 
<p class="p1"> Selection list </p> 
<textarea id="shaixuan" name="check" cols="23" rows="15" class="p2"></textarea> 
<div class="p3"> To participate in the list </div> 
<textarea id="canjia" name="canjia" cols="23" rows="15" class="p4"></textarea> 
<input type="button" value=" copy " class="p5" onclick="getshaixuan();"> 
<!--</textarea> <button onclick="getshaixuan()"> Access to content </button>--> 

</body> 
</html> 

Implementation effect:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/201311191615255.gif? 20131019161541 ">

Related articles: