示例一:
<script type=
"text/javascript"
>
function
copyUrl2()
{
var
Url2=document.getElementById(
"biao1"
);
Url2.select();
// 选择对象
document.execCommand(
"Copy"
);
// 执行浏览器复制命令
alert(
"已复制好,可贴粘。"
);
}
</script>
<textarea cols=
"20"
rows=
"10"
id=
"biao1"
>用户定义的代码区域</textarea>
<input type=
"button"
onClick=
"copyUrl2()"
value=
"点击复制代码"
/>
示例二:
<input type=
"button"
name=
"anniu2"
onClick=
'copyUrl()'
value=
"复制URL地址"
>
<script language=
"javascript"
>
function
copyUrl()
{
var
clipBoardContent=
this
.location.href;
window.clipboardData.setData(
"Text"
,clipBoardContent);
alert(
"复制成功!"
);
}
</script>
示例三:复制专题地址和 url 地址
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=gb2312"
/>
<title>Js复制代码</title>
</head>
<body>
<p>
<input type=
"button"
name=
"anniu1"
onClick=
'copyToClipBoard()'
value=
"复制专题地址和url地址,传给QQ/MSN上的好友"
>
<script language=
"javascript"
>
function
copyToClipBoard(){
var
clipBoardContent=
""
;
clipBoardContent+=document.title;
clipBoardContent+=
""
;
clipBoardContent+=
this
.location.href;
window.clipboardData.setData(
"Text"
,clipBoardContent);
alert(
"复制成功,请粘贴到你的QQ/MSN上推荐给你的好友"
);
}
</script>
示例四:点击文本框时,复制文本框里面的内容
<input onclick=
"oCopy(this)"
value=
"你好.要copy的内容!"
>
<script language=
"javascript"
>
function
oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand(
"Copy"
)
alert(
"复制成功!"
);
}
</script>