//复制黏贴到指定编辑区
function pastetemp() {
var txt="";
txt = document.selection.createRange().text;
var ta = document.getElementById("temptext");
ta.focus();
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
//alert("一定要添加到这行后面才会生效,");
}
ta.document.execCommand("paste"); // 粘贴
} 以上函数一定要添加一个alert()复制粘忒到制定区域才会生效
请问有人碰到过吗? 有的给个说法吧,谢谢大家
function pastetemp() {
var txt="";
txt = document.selection.createRange().text;
var ta = document.getElementById("temptext");
ta.focus();
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
//alert("一定要添加到这行后面才会生效,");
}
ta.document.execCommand("paste"); // 粘贴
} 以上函数一定要添加一个alert()复制粘忒到制定区域才会生效
请问有人碰到过吗? 有的给个说法吧,谢谢大家
如果你没加ALERT,那程序执行那那步的时候,页面也还有加载好,所以找不到对应的元素
加了alert后,有了停留时间,就能够找到 了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD> <BODY>
<script>
//复制黏贴到指定编辑区
function pastetemp() {
var txt=""; txt = document.selection.createRange().text;
var ta = document.getElementById("temptext");
ta.focus();
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
//alert("一定要添加到这行后面才会生效,");
}
ta.document.execCommand("paste"); // 粘贴
} </script><input type="text" value="123"/>
<input id="temptext" type="text" value=""/>
<input type="button" onclick="pastetemp()" value="粘贴"/>
</BODY>
</HTML>
我想应该不是1楼所说的原因,因为以前我也这样认为,通过timeout测试了,仍然没能够解决问题.
观注中.....
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head><body>选择页面文字,会自动复制黏贴到指定编辑区 <br><textarea id=temptext class="codezone" style="width: 100%" rows="12" cols="50"></textarea><br>
qwertyuiopasdfghjkl <script language="javascript">
<!--
//复制黏贴到指定编辑区
function pastetemp() {
var txt= document.selection.createRange().text; //alert(txt)
var ta = document.getElementById("temptext");
ta.focus();
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", txt);
//alert("一定要添加到这行后面才会生效,");
}
ta.document.execCommand("paste"); // 粘贴
}document.onmouseup = function(){
if ((document.selection)&&(document.selection.type == "Text")) {pastetemp()}
}//-->
</script>
</body></html>
或<SCRIPT LANGUAGE="JavaScript" defer>
试试