国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-20 21:26:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

clipboardData 對(duì)象

提供了對(duì)剪貼板的訪問(wèn)。

三個(gè)方法
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ù)制成功,請(qǐng)粘貼到你的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.survivalescaperooms.com" />
<input name=imgurl type=text size=32 value="http://pmp.www.survivalescaperooms.com" />
<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.survivalescaperooms.com</span>  
[<a href="#" onclick="copyText(document.all.tbid)">點(diǎn)擊復(fù)制</a>]<br/><br/>
<span id="tbid2">http://www.www.survivalescaperooms.com/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 += "本資源來(lái)自 " + this.location.href;
            window.clipboardData.setData('Text', content);
            alert("復(fù)制成功,請(qǐng)粘貼到你的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對(duì)象的屬性02</title>
    <script language="javascript" type="text/javascript">
        /*
            window.screen對(duì)象:屏幕對(duì)象,包含了屏幕的相關(guān)信息。
            window.clipboardData對(duì)象:剪貼板對(duì)象,對(duì)剪貼板操作的對(duì)象。(在網(wǎng)頁(yè)內(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 資源來(lái)源:" + window.location.href;
            window.alert('復(fù)制成功!');
            //客戶復(fù)制完之后,這個(gè)屬性才會(huì)顯示內(nèi)容
            window.alert(window.clipboardData.getData("text"));
            window.clipboardData.setData("Text",content);
        }

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

            window.alert("網(wǎng)頁(yè)的內(nèi)容,自能看,不能動(dòng)!");
            return false;
        }
    </script>
</head>
<body onload="screenInfo();" oncopy="forbidCopy();" >
    <form id="form1" runat="server">
    <div id="divClipBoard" onclick="operClipBoard();" >
        //www.survivalescaperooms.com
    </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 沒(méi)有oncopy事件
            body 與 文本框有這個(gè)事件

        */

        function OperClipBoard() {

            window.clipboardData.setData("Text", window.clipboardData.getData("Text") + "/r/n本資源來(lái)自:" + window.location.href);

        }

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 任丘市| 永城市| 郎溪县| 买车| 武川县| 咸阳市| 吴江市| 思南县| 梁平县| 灵丘县| 东乌珠穆沁旗| 屏东市| 怀集县| 富蕴县| 伊春市| 鱼台县| 牟定县| 会东县| 毕节市| 嘉禾县| 南郑县| 西华县| 舟山市| 玛沁县| 南岸区| 昌黎县| 温泉县| 会东县| 二手房| 长春市| 荆州市| 连南| 霍邱县| 株洲市| 申扎县| 澄城县| 聊城市| 双鸭山市| 蒲江县| 台东县| 汤原县|