js 剪下板應用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("代碼已經被成功複製!");

}

 

 

 

 

//設定複製內容 附加 本網站的URL
        function SetCopyContent() {
            window.event.returnValue = false;
            var content = document.title + "/r/n";
            content += document.getElementById("txt1").value + "/r/n";
            content += "本資源來自 " + this.location.href;
            window.clipboardData.setData('Text', content);
            alert("複製成功,請粘貼到你的QQ/MSN上推薦給你的好友");
        }

 

調用:

 <input id="txt1" type="text" value="Hello World!"  onclick="getTxtSelect(event)"/>
        <input type="button" value="複製文字框中的值"  onclick="SetCopyContent();" />

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>Window對象的屬性02</title>    <script language="javascript" type="text/javascript">        /*            window.screen對象:螢幕對象,包含了螢幕的相關資訊。            window.clipboardData對象:剪貼簿對象,對剪貼簿操作的對象。(在網頁內只能支援複製或者設定文字格式設定的)            clearData("Text"):清空剪貼簿            getData("Text"):讀取剪貼簿的值,在IE中只能支援Text文字格式設定            setData("Text",value):設定剪貼簿中的值            案例:複製地址給好友,見備忘            禁止複製:body oncopy事件中 設定 return false;            oncopy、onpase事件:複製、粘貼事件,可用於多數控制項        */                //螢幕解析度        function screenInfo() {            if (window.screen.width < 1024 || window.screen.height < 768) {                window.alert("您的電腦屬於史前產物!");                return;            }            window.alert("您的解析度是:" + window.screen.width + " " + window.screen.height);        }        //複製地址給好友        function operClipBoard() {            var divObj = document.getElementById("divClipBoard");            var content = divObj.innerText;            content = "您複製的內容是:" + content + " \r\n 資源來源:" + window.location.href;            window.alert('複製成功!');            //客戶複製完之後,這個屬性才會顯示內容            window.alert(window.clipboardData.getData("text"));            window.clipboardData.setData("Text",content);        }        //網頁禁止複製        function forbidCopy() {            window.alert("網頁的內容,自能看,不能動!");            return false;        }    </script></head><body onload="screenInfo();" oncopy="forbidCopy();" >    <form id="form1" runat="server">    <div id="divClipBoard" onclick="operClipBoard();" >http://www.baidu.com    </div>    <hr />    輸入密碼:    <input type="text" oncopy="window.alert('禁止複製!');return false;" />    再輸入一邊密碼:    <input type="text" onpaste="window.alert('禁止粘貼!');return false;" />    </form></body></html>
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>       <script language="javascript" type="text/javascript">        /*            div 沒有oncopy事件            body 與 文字框有這個事件                    */                function OperClipBoard() {            window.clipboardData.setData("Text", window.clipboardData.getData("Text") + "\r\n本資源來自:" + window.location.href);        }        /*            流程:先oncopy觸發,觸發後僅僅是將內容複寫到粘貼板,如果需要2次處理的話,等內容複寫到粘貼板後,再進行2次操作,也就是對值進行處理後,            在賦值操作        */        function copyContent()        {            window.setTimeout("OperClipBoard()", 100);        }    </script></head><body>    <form id="form1" runat="server">    <div oncopy="copyContent();">        Hello MyJSWorld!    </div>    <br />    <input type="text" oncopy="OperClipBoard();" value="Hello MyJSWorld!" />    </form></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.