源代碼: 復制代碼 代碼如下:
<!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>內容篩選</title>
<script type='text/javascript'>
function getPositions() {
var el = document.getElementById('shaixuan');
var startPosition = 0;//所選文本的開始位置
var endPosition = 0;//所選文本的結束位置
if(document.selection) {
//IE
var range = document.selection.createRange();//創建范圍對象
var drange = range.duplicate();//克隆對象
drange.moveToElementText(el); //復制范圍
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 獲取textarea中,選中的文本
function getshaixuan() {
var position = getPositions();
var start = position.start;//開始位置
var end = position.end;//結束位置
var text = document.getElementById('shaixuan').value;
var selectText = text.substr(start, (end - start));//textarea中,選中的文本
//alert(selectText);
var textBox1 = document.getElementById("canjia");
textBox1.innerText=textBox1.value+selectText;
}
//]]>
</script>
<title>元素的復制</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">篩選名單</p>
<textarea id="shaixuan" name="check" cols="23" rows="15" class="p2"></textarea>
<div class="p3">參加名單</div>
<textarea id="canjia" name="canjia" cols="23" rows="15" class="p4"></textarea>
<input type="button" value="內容復制" class="p5" onclick="getshaixuan();">
<!--</textarea> <button onclick="getshaixuan()">獲取內容</button>-->
</body>
</html>
實現效果: