欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js 剪切板應(yīng)用clipboardData詳細(xì)解析

 更新時(shí)間:2013年12月17日 10:10:15   作者:  
本篇文章主要介紹了js剪切板應(yīng)用clipboardData。需要的朋友可以過來參考下,希望對大家有所幫助

注意:ie7,與ie8 對網(wǎng)頁有個復(fù)制的權(quán)限,需在“安全”中的“自定義級別”的腳本中設(shè)置

clipboardData 對象

提供了對剪貼板的訪問。

三個方法
1.clearData(sDataFormat) 刪除剪貼板中指定格式的數(shù)據(jù)。
2.getData(sDataFormat) 從剪貼板獲取指定格式的數(shù)據(jù)。
3.setData(sDataFormat, sData) 給剪貼板賦予指定格式的數(shù)據(jù)。返回 true 表示操作成功。

例子

<script language="JavaScript">
<!--
var text = "123";
if (!window.clipboardData.setData('Text', text)) // 賦予 text 格式的數(shù)據(jù)
{
     alert("復(fù)制失敗!");
}
text = window.clipboardData.getData('Text'); // 獲取 text 格式的數(shù)據(jù)
alert(text);
window.clipboardData.clearData('Text'); // 清除 text 格式的數(shù)據(jù)
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復(fù)制代碼</title>
</head>
<body>
<p>
<input type="button" name="anniu1" onClick='copyToClipBoard()' value="復(fù)制專題地址和url地址,傳給QQ/MSN上的好友">
<script language="javascript">
   function copyToClipBoard(){
    var clipBoardContent="";
    clipBoardContent+=document.title;
    clipBoardContent+="";
    clipBoardContent+=this.location.href;
    window.clipboardData.setData("Text",clipBoardContent);
    alert("復(fù)制成功,請粘貼到你的QQ/MSN上推薦給你的好友");
}
</script>  
<br />
<br />


直接復(fù)制url
<input type="button" name="anniu2" onClick='copyUrl()' value="復(fù)制URL地址">
<script language="javascript">
   function copyUrl()
   {
    var clipBoardContent=this.location.href;
    window.clipboardData.setData("Text",clipBoardContent);
    alert("復(fù)制成功!");
   }
</script>
<br/>
<br/>
點(diǎn)擊文本框時(shí),復(fù)制文本框里面的內(nèi)容
<input onclick="oCopy(this)" value="你好.要copy的內(nèi)容!">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("復(fù)制成功!");
}
</script>
<br />
<br />
復(fù)制文本框或者隱藏域中的內(nèi)容
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();  
js=myimg.createTextRange();  
js.execCommand("Copy");
alert("復(fù)制成功!");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();  
js.execCommand("Copy");
alert("復(fù)制成功!");
}
</script>

<input name=myimg type=hidden id=myimg value="http://pmp.www.dbjr.com.cn" />
<input name=imgurl type=text size=32 value="http://pmp.www.dbjr.com.cn" />
<input type=button value="點(diǎn)擊這里復(fù)制本站地址" onclick="CopyUrl(imgurl);" />


<br />
<br/>
復(fù)制span標(biāo)記中的內(nèi)容
<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("復(fù)制成功!");
}
</script>

以下是代碼片段:<br />
<br />
<span id="tbid">http://pmp.www.dbjr.com.cn</span>  
[<a href="#" onclick="copyText(document.all.tbid)">點(diǎn)擊復(fù)制</a>]<br/><br/>
<span id="tbid2">http://www.www.dbjr.com.cn/pmp</span>  
[<a href="#" onclick="copyText(document.all.tbid2)">點(diǎn)擊復(fù)制</a>]<br/><br/>

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

還有一種方法:

function copyQQ(qq){
var obj=document.getElementById(qq);
obj.select();
     js=obj.createTextRange();
     js.execCommand("Copy");
     alert("代碼已經(jīng)被成功復(fù)制!");

}


//設(shè)置復(fù)制內(nèi)容 附加 本網(wǎng)站的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("復(fù)制成功,請粘貼到你的QQ/MSN上推薦給你的好友");
        }

調(diào)用:

<input id="txt1" type="text" value="Hello World!"  onclick="getTxtSelect(event)"/>
<input type="button" value="復(fù)制文本框中的值"  onclick="SetCopyContent();" />

復(fù)制代碼 代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Window對象的屬性02</title>
    <script language="javascript" type="text/javascript">
        /*
            window.screen對象:屏幕對象,包含了屏幕的相關(guān)信息。
            window.clipboardData對象:剪貼板對象,對剪貼板操作的對象。(在網(wǎng)頁內(nèi)只能支持復(fù)制或者設(shè)置文本格式的)
            clearData("Text"):清空剪貼板
            getData("Text"):讀取剪貼板的值,在IE中只能支持Text文本格式
            setData("Text",value):設(shè)置剪貼板中的值
            案例:復(fù)制地址給好友,見備注
            禁止復(fù)制:body oncopy事件中 設(shè)置 return false;
            oncopy、onpase事件:復(fù)制、粘貼事件,可用于多數(shù)控件
        */

        //屏幕分辨率
        function screenInfo() {

            if (window.screen.width < 1024 || window.screen.height < 768) {
                window.alert("您的電腦屬于史前產(chǎn)物!");
                return;
            }
            window.alert("您的分辨率是:" + window.screen.width + " " + window.screen.height);

        }

        //復(fù)制地址給好友
        function operClipBoard() {

            var divObj = document.getElementById("divClipBoard");
            var content = divObj.innerText;
            content = "您復(fù)制的內(nèi)容是:" + content + " \r\n 資源來源:" + window.location.href;
            window.alert('復(fù)制成功!');
            //客戶復(fù)制完之后,這個屬性才會顯示內(nèi)容
            window.alert(window.clipboardData.getData("text"));
            window.clipboardData.setData("Text",content);
        }

        //網(wǎng)頁禁止復(fù)制
        function forbidCopy() {

            window.alert("網(wǎng)頁的內(nèi)容,自能看,不能動!");
            return false;
        }
    </script>
</head>
<body onload="screenInfo();" oncopy="forbidCopy();" >
    <form id="form1" runat="server">
    <div id="divClipBoard" onclick="operClipBoard();" >
        http://www.dbjr.com.cn
    </div>
    <hr />
    輸入密碼:
    <input type="text" oncopy="window.alert('禁止復(fù)制!');return false;" />
    再輸入一邊密碼:
    <input type="text" onpaste="window.alert('禁止粘貼!');return false;" />
    </form>
</body>
</html>


復(fù)制代碼 代碼如下:

<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觸發(fā),觸發(fā)后僅僅是將內(nèi)容復(fù)制到粘貼板,如果需要2次處理的話,等內(nèi)容復(fù)制到粘貼板后,再進(jìn)行2次操作,也就是對值進(jìn)行處理后,
            在賦值操作
        */
        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>

相關(guān)文章

最新評論