如何在网页中用代码实现多个“复制到剪贴板”功能?

在知道里找的是:
-------------------------------以下是代码
<script type="text/javascript">
function jsCopy(){
var e=document.getElementById("contents");//对象是contents
e.select(); //选择对象
document.execCommand("Copy"); //执行浏览器复制命令
}
</script>
<textarea name="contents" cols="40" rows="5"></textarea>
<br />
<input type="button" onClick="jsCopy();" value="复制" />
---------------------------------以上是代码

很好用,但我要实现一个页面下出现多个次功能,应该如何更改代码?
比如说,我现在有以上面的代码在一个页面下实现4处此功能,代码该如何搞?我复制4处出来,但实际的作用还是只复制最初的那一处,我知道是name的问题,但却不知道怎么改。
(那位兄台帮我用上面的代码搞一下,直接搞出4处能用的,每排2个,搞2排,要绝对精简的)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>

<table width="600" border="0" cellspacing="0" cellpadding="0">

<tr>
<td>
<script>
function copyit(){
document.all.text.select();
document.execCommand("copy");
alert("成功复制文字1");
}
</script>
<textarea name="text" cols="40" rows="5"></textarea>
<input name="select" type="button" onclick="copyit()" value="复制1" />

</td>
<td>
<script>
function copyit2(){
document.all.text2.select();
document.execCommand("copy");
alert("成功复制文字2");
}
</script>
<textarea name="text2" cols="40" rows="5"></textarea>
<input name="select2" type="button" onclick="copyit2()" value="复制2" />

</td>
</tr>
<tr>
<td>
<script>
function copyit3(){
document.all.text3.select();
document.execCommand("copy");
alert("成功复制文字3");
}
</script>
<textarea name="text3" cols="40" rows="5"></textarea>
<input name="select3" type="button" onclick="copyit3()" value="复制3" />
</td>

<td>
<script>
function copyit4(){
document.all.text4.select();
document.execCommand("copy");
alert("成功复制文字4");
}
</script>
<textarea name="text4" cols="40" rows="5"></textarea>
<input name="select4" type="button" onclick="copyit4()" value="复制4" />
</td>
</tr>
</table>

</body>
</html>

我的网站:www.raydetect.com
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-01-14
<script type="text/javascript">
function jsCopy(){
var e=document.getElementsByName("contents");
var i;
var str = "";
for(i=0;i < e.length ; i++){
if(i==0){
str += e[i].value;
}else if(i==1 || i==3){
str += "\t";
str += e[i].value;
}else if(i==2){
str += "\n";
str += e[i].value;
}
}
var sel=document.getElementById("submitTextArea");
sel.value=str;
sel.select();
document.execCommand("Copy");
}
</script>
<table>
<tr>
<td align="left" valign="top"><textarea name="contents" cols="40" rows="5"></textarea></td>
<td align="left" valign="top"><textarea name="contents" cols="40" rows="5"></textarea></td>
</tr>
<tr>
<td align="left" valign="top"><textarea name="contents" cols="40" rows="5"></textarea></td>
<td align="left" valign="top"><textarea id="submitTextArea" style="position:absolute;"></textarea><textarea name="contents" cols="40" rows="5" style="position:absolute;"></textarea></td>
</tr>
</table>
<input type="button" onClick="jsCopy();" value="复制" />