js 实现 将网页内容 复制到剪切板上

给大家推荐个靠谱的公众号程序员探索之路,大家一起加油

一、实现点击按钮,复制文本框中的的内容

1

2

3

4

5

6

7

8

9

10

11

<script type="text/javascript">

function copyUrl2()

{

varUrl2=document.getElementById("biao1");

Url2.select();// 选择对象

document.execCommand("Copy");// 执行浏览器复制命令

alert("已复制好,可贴粘。");

}

</script>

<textarea cols="20"rows="10"id="biao1">用户定义的代码区域</textarea>

<input type="button"onClick="copyUrl2()"value="点击复制代码"/>

 二、复制专题地址和 url 地址,传给 QQ/MSN 上的好友

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!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(){

varclipBoardContent="";

clipBoardContent+=document.title;

clipBoardContent+="";

clipBoardContent+=this.location.href;

window.clipboardData.setData("Text",clipBoardContent);

alert("复制成功,请粘贴到你的QQ/MSN上推荐给你的好友");

}

</script>

三、直接复制 url

1

2

3

4

5

6

7

8

9

<input type="button"name="anniu2"onClick='copyUrl()'value="复制URL地址">

<script language="javascript">

function copyUrl()

{

varclipBoardContent=this.location.href;

window.clipboardData.setData("Text",clipBoardContent);

alert("复制成功!");

}

</script>

四、点击文本框时,复制文本框里面的内容

1

2

3

4

5

6

7

8

9

<input onclick="oCopy(this)"value="你好.要copy的内容!">

<script language="javascript">

function oCopy(obj){

obj.select();

js=obj.createTextRange();

js.execCommand("Copy")

alert("复制成功!");

}

</script>

五、复制文本框或者隐藏域中的内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<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>

六、复制 span 标记中的内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<script type="text/javascript">

</script>

<br />

<br />

<script type="text/javascript">functioncopyText(obj)

{

varrng = document.body.createTextRange();

rng.moveToElementText(obj);

rng.scrollIntoView();

rng.select();

rng.execCommand("Copy");

rng.collapse(false);

alert("复制成功!");

}

</script>

七、浏览器兼容  copyToClipboard("拷贝内容")

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

function copyToClipboard(txt) {

   if(window.clipboardData) {

    window.clipboardData.clearData();

    clipboardData.setData("Text", txt);

    alert("复制成功!");

  

   }elseif (navigator.userAgent.indexOf("Opera") != -1) {

    window.location = txt;

   }elseif (window.netscape) {

    try{

     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

    }catch(e) {

     alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'");

    }

    varclip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);

    if(!clip)

     return;

    vartrans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);

    if(!trans)

     return;

    trans.addDataFlavor("text/unicode");

    varstr = newObject();

    varlen = newObject();

    varstr = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

    varcopytext = txt;

    str.data = copytext;

    trans.setTransferData("text/unicode", str, copytext.length * 2);

    varclipid = Components.interfaces.nsIClipboard;

    if(!clip)

     returnfalse;

    clip.setData(trans,null, clipid.kGlobalClipboard);

    alert("复制成功!");

   }

  }

八、兼容各大浏览器的复制代码(结合ZeroClipboard.js)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<html>

<head>

<title>Zero Clipboard Test</title>

<script type="text/javascript"src="ZeroClipboard.js"></script>

<script language="JavaScript">

 varclip = null;

 function$(id) { returndocument.getElementById(id); }

 functioninit() {

   clip = newZeroClipboard.Client();

   clip.setHandCursor(true); 

   clip.addEventListener('mouseOver',function(client) {

 // update the text on mouse over

 clip.setText( $('fe_text').value );

   });

    

   clip.addEventListener('complete',function(client, text) {

 //debugstr("Copied text to clipboard: " + text );

 alert("该地址已经复制,你可以使用Ctrl+V 粘贴。");

   });

   clip.glue('clip_button','clip_container');

 }

</script>

</head>

<body onLoad="init()">

<input id="fe_text"cols=50 rows=5 value=复制内容文本1 >

<span id="clip_container"><span id="clip_button"><b>复制</b></span></span>

</body>

</html

没有更多推荐了,返回首页