clipboardData 對象的應用執行個體

來源:互聯網
上載者:User

 

注意:ie7,與ie8 對網頁有個複製的許可權,需在“安全”中的“自訂層級”的指令碼中設定

clipboardData 對象

提供了對剪貼簿的訪問。

三個方法
1.clearData(sDataFormat) 刪除剪貼簿中指定格式的資料。
2.getData(sDataFormat) 從剪貼簿擷取指定格式的資料。
3.setData(sDataFormat, sData) 給剪貼簿賦予指定格式的資料。返回 true 表示操作成功。

例子

<script language="JavaScript">
<!--
var text = "123";
if (!window.clipboardData.setData('Text', text)) // 賦予 text 格式的資料
{
     alert("複製失敗!");
}
text = window.clipboardData.getData('Text'); // 擷取 text 格式的資料
alert(text);
window.clipboardData.clearData('Text'); // 清除 text 格式的資料
text = window.clipboardData.getData('Text');
alert(text);
//-->
</script>

一些方法:

<!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>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>  
<br />
<br />

直接複製url
<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>
<br/>
<br/>
點擊文字框時,複製文字框裡面的內容
<input onclick="oCopy(this)" value="你好.要copy的內容!">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("複製成功!");
}
</script>
<br />
<br />
複製文字框或者隱藏欄位中的內容
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();  
js=myimg.createTextRange();  
js.execCommand("Copy");
alert("複製成功!");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();  
js.execCommand("Copy");
alert("複製成功!");
}
</script>

<input name=myimg type=hidden id=myimg value="http://pmp.cnblogs.com" />
<input name=imgurl type=text size=32 value="http://pmp.cnblogs.com" />
<input type=button value="點擊這裡複製本站地址" onclick="CopyUrl(imgurl);" />

<br />
<br/>
複製span標記中的內容
<script type="text/javascript">
</script>
<br />
<br />
<script type="text/javascript">function copyText(obj)  
{
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
alert("複製成功!");
}
</script>

以下是程式碼片段:<br />
<br />
<span id="tbid">http://pmp.cnblogs.com</span>  
[<a href="#" onclick="copyText(document.all.tbid)">點擊複製</a>]<br/><br/>
<span id="tbid2">http://www.cnblogs.com/pmp</span>  
[<a href="#" onclick="copyText(document.all.tbid2)">點擊複製</a>]<br/><br/>

</p>
</body>
</html>

 

 

還有一種方法:

function copyQQ(qq){
var obj=document.getElementById(qq);
obj.select();
     js=obj.createTextRange();
     js.execCommand("Copy");
     alert("代碼已經被成功複製!");

}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.