js 剪切板應用clipboardData詳細解析
注意:ie7,與ie8 對網頁有個復制的權限,需在“安全”中的“自定義級別”的腳本中設置
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("復制失敗!");
}
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復制代碼</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.chabaoo.cn" />
<input name=imgurl type=text size=32 value="http://pmp.chabaoo.cn" />
<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.chabaoo.cn</span>
[<a href="#" onclick="copyText(document.all.tbid)">點擊復制</a>]<br/><br/>
<span id="tbid2">http://www.chabaoo.cn/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事件:復制、粘貼事件,可用于多數(shù)控件
*/
//屏幕分辨率
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://chabaoo.cn
</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觸發(fā),觸發(fā)后僅僅是將內容復制到粘貼板,如果需要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>
- Firefox中通過JavaScript復制數(shù)據(jù)到剪貼板(Copy to Clipboard 跨瀏覽器版)
- 代碼塊高亮可復制顯示js插件highlight.js+clipboard.js整合
- Vue使用Clipboard.JS在h5頁面中復制內容實例詳解
- 在vue使用clipboard.js進行一鍵復制文本的實現(xiàn)示例
- js剪切板應用clipboardData實例解析
- JS基于clipBoard.js插件實現(xiàn)剪切、復制、粘貼
- clipboard.js無需Flash無需依賴任何JS庫實現(xiàn)文本復制與剪切
- Clipboard.js 無需Flash的JavaScript復制粘貼庫
- JS如何使用剪貼板操作Clipboard API
相關文章
JavaScript自定義瀏覽器滾動條兼容IE、 火狐和chrome
本文主要分享了使用原生JavaScript實現(xiàn)自定義瀏覽器滾動條兼容IE、 火狐和chrome的思路與方法,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01javascript當onmousedown、onmouseup、onclick同時應用于同一個標簽節(jié)點Element
先通過一個簡單例子測試并發(fā)現(xiàn)我說的問題,讓你有個直觀的印象,再接著看我的解決辦法。2010-01-01JavaScript實現(xiàn)倒計時跳轉頁面功能【實用】
本文分享了JavaScript實現(xiàn)倒計時跳轉頁面功能的具體實例代碼,頁面代碼簡單,直接拷貝就能運行,頁面可以自己美化下哦。需要的朋友一起來看下吧2016-12-12深入理解JavaScript系列(13) This? Yes,this!
在這篇文章里,我們將討論跟執(zhí)行上下文直接相關的更多細節(jié)。討論的主題就是this關鍵字。實踐證明,這個主題很難,在不同執(zhí)行上下文中this的確定經常會發(fā)生問題2012-01-01