js 剪切板應(yīng)用clipboardData詳細(xì)解析
注意: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();" />
<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>
<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>
- Firefox中通過JavaScript復(fù)制數(shù)據(jù)到剪貼板(Copy to Clipboard 跨瀏覽器版)
- 代碼塊高亮可復(fù)制顯示js插件highlight.js+clipboard.js整合
- Vue使用Clipboard.JS在h5頁面中復(fù)制內(nèi)容實(shí)例詳解
- 在vue使用clipboard.js進(jìn)行一鍵復(fù)制文本的實(shí)現(xiàn)示例
- js剪切板應(yīng)用clipboardData實(shí)例解析
- JS基于clipBoard.js插件實(shí)現(xiàn)剪切、復(fù)制、粘貼
- clipboard.js無需Flash無需依賴任何JS庫實(shí)現(xiàn)文本復(fù)制與剪切
- Clipboard.js 無需Flash的JavaScript復(fù)制粘貼庫
- JS如何使用剪貼板操作Clipboard API
相關(guān)文章
JavaScript自定義瀏覽器滾動條兼容IE、 火狐和chrome
本文主要分享了使用原生JavaScript實(shí)現(xiàn)自定義瀏覽器滾動條兼容IE、 火狐和chrome的思路與方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01javascript當(dāng)onmousedown、onmouseup、onclick同時(shí)應(yīng)用于同一個標(biāo)簽節(jié)點(diǎn)Element
先通過一個簡單例子測試并發(fā)現(xiàn)我說的問題,讓你有個直觀的印象,再接著看我的解決辦法。2010-01-01jQuery判斷密碼強(qiáng)度實(shí)現(xiàn)思路及代碼
實(shí)現(xiàn)思路為:密碼小于六位的時(shí)候,密碼強(qiáng)度圖片都為灰色;密碼為七位及以上并且字母、數(shù)字、特殊字符三項(xiàng)中有兩項(xiàng),強(qiáng)度等等,感興趣的朋友可以參考下哈2013-04-04JavaScript實(shí)現(xiàn)倒計(jì)時(shí)跳轉(zhuǎn)頁面功能【實(shí)用】
本文分享了JavaScript實(shí)現(xiàn)倒計(jì)時(shí)跳轉(zhuǎn)頁面功能的具體實(shí)例代碼,頁面代碼簡單,直接拷貝就能運(yùn)行,頁面可以自己美化下哦。需要的朋友一起來看下吧2016-12-12深入理解JavaScript系列(13) This? Yes,this!
在這篇文章里,我們將討論跟執(zhí)行上下文直接相關(guān)的更多細(xì)節(jié)。討論的主題就是this關(guān)鍵字。實(shí)踐證明,這個主題很難,在不同執(zhí)行上下文中this的確定經(jīng)常會發(fā)生問題2012-01-01微信小程序自定義tab實(shí)現(xiàn)多層tab嵌套
這篇文章主要為大家詳細(xì)介紹了微信小程序自定義tab實(shí)現(xiàn)多層tab嵌套,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07