javascript中clipboardData對象用法

來源:互聯網
上載者:User

javascript中clipboardData對象用法

   本文執行個體講述了javascript中clipboardData對象用法。分享給大家供大家參考。具體分析如下:

  clipboardData對象 ,注意網頁裡剪貼簿到現在只能設定Text類型,即只能複製文本

  clearData("Text")清空粘貼板

  getData("Text")讀取粘貼板的值

  setData("Text",val)設定粘貼板的值

  當複製的時候body的oncopy事件被觸發,直接return false就是禁止複製,注意是不能複製網頁裡的文本了

  

  很多元素也有oncopy,onpaste事件

  1.複製文本到剪貼簿

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function CopyLinkAddress() {

clipboardData.setData("Text", "請複製網址到您的QQ:" + location.href);

alert("複製成功!");

}

</script>

</head>

<body>

<input type="button" value="複製網址" onclick="CopyLinkAddress()" />

</body>

</html>

  2.禁止複製,和禁止粘貼

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function CopyLinkAddress() {

clipboardData.setData("Text", "請複製網址到您的QQ:" + location.href);

alert("複製成功!");

}

</script>

</head>

<!--<body oncopy="alert('禁止複製');return false;">-->

<body>

<input type="button" value="複製網址" onclick="CopyLinkAddress()" />

測試複製的文本<br />

手機號碼1:<input type="text" /><br />

手機號碼2:<input type="text"

onpaste="alert('禁止粘貼,必須手工錄入!');return false;" />

</body>

</html>

  3.clipboardData對象複製時添加來源

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript">

function ModifyCopyData() {

clipboardData.setData('Text',clipboardData.getData('Text') +

'rn來自Pigeon網站' + location.href);

}

</script>

</head>

<!--不能直接在oncopy中調用ModifyCopyData函數

需設定定時器,0.1秒後執行,這樣就不再oncopy的執行呼叫堆疊上了

-->

<body oncopy="setTimeout('ModifyCopyData()',100)">

指令碼之家:www.jb51.net

</body>

</html>

  希望本文所述對大家的C#程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.